Scan Multiple Inputs from the Same Line with scanf

I am trying to scan in one string input and save it in a structure. I am not sure if I am storing it incorrectly or I am printing it incorrectly. I'm not sure how to use scanf with for loops, since I never did this before not mentioning that C likes to rewrite stuff. Therefore, I was not sure how to approach this.

This is what I put together, but when I type, I get unnecessary numbers. I was going to probably use pointers, but my professor will not let us use it. That is why I have problems.

EDITED

 #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 3 #define MAXTRIP 6 struct stop { float cost; float time; }; struct trip { char Dest_letter; int stop_num; struct stop leg[MAX]; }; int main(void) { int trip_num, index, i; struct trip travel[MAXTRIP]; printf("Enter number of trips: "); scanf("%d", &trip_num); printf("Please enter destination letter/number of stops and cost/length of each stop:\n"); for (index = 0; index < trip_num; index++) { scanf("%c %d", &travel[index].Dest_letter, &travel[index].stop_num); for ( i=0; i < travel[index].stop_num; i++) scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time); } for (index =0; index < trip_num; index++) { printf("Trip:%d \nDestination Letter:%c", index+1, travel[index].Dest_letter); for (i=0; i < travel[index].stop_num; i++) printf("Cost:%.2f \nLength:%.2f", travel[index].leg[i].cost, travel[index].leg[i].time); } } 
+4
source share
2 answers

You have a print loop inside your read loop. He tries to print all the trip information after reading the first one.

Edit: The problem is that the scanf method handles individual characters, is not intuitive next to how it handles strings and numbers. It reads the next character from the standard, which is probably a newline when you finish typing the previous input. He then tries to try to read the integer, but instead finds the letter you intended to use, %c . This causes a scanf error and does not initialize stop_num .

In one case, this can be read in a line. scanf will start reading a line in the first character without spaces and will stop reading it with the first space character. Then just grab the first character from the buffer into which you are reading the line.

 #include <stdio.h> #define MAX 3 #define MAXTRIP 6 struct stop { float cost; float time; }; struct trip { char Dest_letter; int stop_num; struct stop leg[MAX]; }; int main(void) { int trip_num, index, i; struct trip travel[MAXTRIP]; char buffer[10]; printf("Enter number of trips: "); scanf("%d", &trip_num); for (index = 0; index < trip_num; index++) { printf("Please enter destination letter/number of stops:\n"); scanf("%s %d", buffer, &travel[index].stop_num); travel[index].Dest_letter = buffer[0]; for (i = 0; i < travel[index].stop_num; i++){ printf("Please enter cost/length of stop %d:\n", i); scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time); } } printf("%d trips\n", trip_num); for (index = 0; index < trip_num; index++) { printf("Trip:%d \nDestination Letter:%c\n", index + 1, travel[index].Dest_letter); for (i = 0; i < travel[index].stop_num; i++) printf("Cost:%.2f \nLength:%.2f\n", travel[index].leg[i].cost, travel[index].leg[i].time); } } 
+2
source

scanf() used to get values ​​for runtime and is used in the control line

 main() { //this RMVIVEK coding for Scaning Multiple inputs from one line using scanf int r,m,v1,i,v,e,k; char a,b,c; float x,y,z; clrscr(); printf("enter the Your five subject marks"); //%d is integer format , scanf("%d%d%d%d%d",&r,&m,&v,&i,&e); //%c is char format and %s is a sting formar printf("enter the any character values"); scanf("%c%c%c",a,b,c); //%f is float format printf("enter the Your height and wight"); scanf("%f%f",&x,&y); } 
+2
source

Source: https://habr.com/ru/post/1345481/


All Articles