why doesn't this store the number of kms in the struc? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

why doesn't this store the number of kms in the struc?

https://code.sololearn.com/cUpTpUQp1LLP/#c

16th Dec 2021, 8:12 PM
Ishaan Oberoi
Ishaan Oberoi - avatar
4 Respostas
+ 4
The scanf() in line 14 leaves a newline in the input buffer. After that, the next fgets() only sees the newline and stops reading. Thereafter the remaining inputs are all mismatched. Solution: have the first scanf() (line 14) dump the newline. Change from: scanf("%d",&n) To: scanf("%d\n",&n) And do a similar fix for reading kms in line 33.
16th Dec 2021, 9:17 PM
Brian
Brian - avatar
+ 2
Ishaan Oberoi in some systems the end of line has two characters. That makes it tricky to recommend a universal solution. It might be better to replace scanf() with fgets() to retrieve the line string into a temporary char array, and then use sscanf() to read the number from the temporary string. That should handle the input consistently across platforms.
17th Dec 2021, 4:00 AM
Brian
Brian - avatar
+ 1
Brian thanks I tweaked it a little and it worked
17th Dec 2021, 3:46 AM
Ishaan Oberoi
Ishaan Oberoi - avatar
0
it's storing. I tried this input: 1 a b c 10
16th Dec 2021, 8:33 PM
Anya
Anya - avatar