c - Scaning Multiple inputs from one line using scanf -


I am trying to scan in a string and trying to store it in a straight one. I am not sure whether I am storing it wrong or I am printing it wrongly. I'm sure that since using the scanf for for loops I have not ever done that I do not like to overwrite stuff to mention C . So I was not sure how to contact this approach.

It is something I keep together, but when I print it, I get a junk number. I probably wanted to use pointers but my professor would not let us use it. That is why I am having trouble.

Edit

  #include & lt; Stdio.h & gt; #include & lt; String.h & gt; # Include & lt; Stdlib.h & gt; # Defined Maximum 3 # Defined MAXTRIP Stretch 6 {Float Cost; Float time; }; Straight trip {char Dest_letter} Int stop_num; Straight stop leg [max]; }; Int main (zero) {int trip_num, index, i; Architectural travel trip [MaxTrip]; Printf ("Enter the number of visits:"); Scanf ("% d", and trip_num); Printf ("Please enter the destination letter / stop number and the cost / length of each stop: \ n"); (Index = 0; index & lt; trip_num; index ++) {scanf ("% c% d", and travel [index] .inst_letter, and travel [index] .stop_num); (I = 0; i & lt; travel [index] .stop_num; i ++) scanf ("% f% f", and travel [index] .leg [i]., And travel [index] .leg [ time I); } (Index = 0; index & lt; trip_num; index ++) {printf ("Trip:% d \ n Destination Letter:% C", Index + 1, Travel [Index] .Dest_letter); (I = 0; i [lt; travel] .stop_num; i ++) printf ("cost:% .2f \ nlength:% .2f", travel [index] .leg [i]. , Travel [index] .leg [i] .time); }}    

Your printing loop is within your reading loop. It is trying to print all travel information after reading for the first time.

Edit: The problem here is that the way the scanf handles is unique, the way it handles the strings and numbers, the next character from the standard Reads, which is probably a new letter when you have finished typing the previous input. Then it tries to try an integer, but instead instead it finds the letter which you consume by using the % c . This causes scanf to fail and can not start stop_num .

A way around it might be to read in the string instead. The first non-whitespace character will start reading scanf and stop reading it on the first whitespace character. After that simply take the first letter from the buffer that you read in the string.

  #include & lt; Stdio.h & gt; # Defined Maximum 3 # Defined MAXTRIP Stretch 6 {Float Cost; Float time; }; Straight trip {char Dest_letter} Int stop_num; Straight stop leg [max]; }; Int main (zero) {int trip_num, index, i; Architectural travel trip [MaxTrip]; Four buffers [10]; Printf ("Enter the number of visits:"); Scanf ("% d", and trip_num); (Index = 0; index & lt; trip_num; index ++) for {printf ("Please enter the destination letter / stop number: \ n"); Scanf ("% s% d", buffer, and travel [index] .stop_num); Travel [index] .inst_letter = buffer [0]; (I = 0; i & lt; travel [index] .stop_num; i ++) {printf ("Please enter the cost / length of% d: \ n", i); Scanf ("% f% f", and travel [index] .leg [i]. Coast, and travel [index] .leg [i]. Time); }} Printf ("% d trips \ n", trip_num); (Index = 0; index & lt; trip_num; index ++) {printf ("Trip:% d \ n Destination letter:% c \ n", index + 1, travel [index] .inst_letter); (I = 0; i & lt; travel [index] .stop_num; i ++) printf ("cost:% .2f \ nLength:% .2f \ n", travel [index] .leg [i] Travel [index] .leg [i] .time); }}    

Comments