@nitya I dont think so, I tried using fflush(stdin) to clear the input stream, but that didn’t seem to work. I am not so sure if that can be done. Just in case you get to know how to use fflush() to solve this then please do let me know as well. Thanks
When you press ENTER in c it takes it as a character and for %c you store enter. So garbage values are coming. What u need to do is flush the standard input.
Just type fflush(stdin); before each scanf() statement.
From what I understand, the modification provided by ravi works because the \n in scanf eats the white space you provide in between the two inputs which is the new line(\n) character in this case. Since you hit ENTER after the first series of inputs and the waiting place holder is %c - it reads \n from buffer and moves to the next input(correct me if I am wrong).
You can also put a space to eat white space instead of \n like
scanf("%c %f %d ",&b1.name,&b1.price,&b1.page);
scanf("%c %f %d ",&b2.name,&b2.price,&b2.page);
scanf("%c %f %d",&b3.name,&b3.price,&b3.page);
Another alternative is to clear the buffer using fflush like -
@grvana it just eats up the enter key that you press after your first input, so that the value of your character of the second scanf doesn’t take the value \n, but the desired character.
@thezodiac I tried this and it works in my code blocks now… But initially when this question was posted I was using Dev-C++ and it didnt work in it. Does this mean that this is not a kind of “universally accepted” solution, and might not work everywhere. Just like it doesnt work in nitya’s code blocks???
Whitespace character: the function
will read and ignore any whitespace
characters encountered before the next
non-whitespace character (whitespace
characters include spaces, newline and
tab characters – see isspace). A
single whitespace in the format string
validates any quantity of whitespace
characters extracted from the stream
(including none).
Which means always use space before %c in scanf if you want to ensure that the character read is not a white space.