0
How do fix it to what I want
https://code.sololearn.com/cVfb845vMbDe/?ref=app 1. When I run this code on the code block c compiler on my computer and insert instead of number character it started to looping without stoping. why is that. 2. When I tried to make the "answer" character a char type and in the while function in the end of the code I wrote (answer='Y' || answer='y'); it just skipped the scanf function and end the program why is that?
13 Réponses
+ 1
1) When using scanf, you have to make sure that input MATCH with expect format string else scanf will do nothing. In your case this because your program expect 2 floats (at start) but your input starts with a char, the first 2 scanf will do nothing (and char will keep avaible for next scanfs). Then your scanf an int (for get the operation) and here also your scanf will do nothing because input is a char. Next you use scanf for get the answer (another int) and again nothing will happen... At end, probably, you will end with an infinite loop because the char input. You can use the return value of char that give you the number of values correctly readed (if you want read one value and it return 0, something of bad is happen in input)
2) Remember: answer='Y' is an assignement, answer=='Y' is a comparition 😉
+ 1
Maybe you didnt understood... I try to explain in better way with a sample code.
First, load this code https://code.sololearn.com/cp17Csvx8uRZ/#c
That code simply read 3 float (using a loop) and for any read print the value of a "f" var (where scanf WOULD put the readed float) and the result of scanf).
Get us run it with following input line:
Input:
1.0 2 5
Output:
f= 1.000000; i=1
f= 2.000000; i=1
f= 5.000000; i=1
Here all its ok. Now, try to run with:
Input:
1.0 c
Output will be:
f= 1.000000; i=1
f= 1.000000; i=0
f= 1.000000; i=0
Why? First note the "i" value. That represent scanf return value for corrispective iteration. Well, get us to understand whats happened. At first iteration, scanf TRY to read from input a float, and because it read next "value" while white space is encountered, it get 1.0 thats a valid float value (it can be converted to float). Then, at this iteration, scanf INSERT 1.0 value into "f" var and return 1 (the "i" var) for indicate that it readed correctly a value. At this point, the input is constitued by string "c".
At second iteration, scanf TRY again to read a double BUT, this time, the token (whitespace delimited) is NOT a valid float-convertable string, then, scanf CANNOT PUT some value in "f" and return 0 for indicate that NO VALUE IS READED. Take note that input is AGAIN constitued by "c" string because no read is happened. Because of this, in the third iteration SAME what happen like second iteration (scanf cannot read value, "i" is 0 and input keep always "c"). In base to this we can say that while scanf dont read that "c" (by reading a char or a string), every call to scanf CANNOT read nothing, then, can cause an infinite loop if you dont handle this problem
+ 1
... CONTINUE ...
Now lets try with another input:
Input:
1.0
Output:
f= 1.000000; i=1
f= 1.000000; i=-1
f= 1.000000; i=-1
What happened this time? At first iteration, all its ok BUT, because all its readed, input is now empty (or better its in EOF state). Then, what happen if you try to read to an empty input stream with scanf? Well, because nothing can be readed, scanf CANNOT read nothing and return a specific value, -1 (named EOF) that indicate you that stream is at end and you cannot read nothing more.
Like you can see, scanf get us more informations that you can think, then use they for handle some particular context (like unexcpected input or/and EOF input)
2) I repeat. This is an assignement
answer= 'Y'
This, instead, is a comparation between two values
answer=='Y'
The first ASSIGN a value to "answer" var and, if cast to bool is requested (like in condition of while) it "return" a value that its valutated to false only if byte representation is 0 (in this case an '\0' char) else is ALWAYS true.
The second its valutated to true only if answer is equals to 'Y' char
I hope that now its more clear. If you have some doubt, ask
+ 1
Wow first of all thanks a lot. I learned a lot from your explanation and start to understand a lot better how C works.
now there is still something I didn't understand. the answer variable in my program is a char type so why can't he read the y/Y letter?
+ 1
ariel You declared answer like int... Edit your code with char (and relative while condition) and only aftrr i can said where is the problem
P.S. The code that you linked to top of this discussion contains invalid chars that make your code not-compilable.... Please, make necessary adjustament because i writing from a telephone and its not very well write too much 😁
+ 1
Well, this is another "confusion" caused by scanf... When it read integer/float types, it consume (read but dont assign) automatically whitespaces (if any) before TRY to read the value BUT this not happen with other types (like char). Lets go with another example. Imagine that you use 3 DIFFERENTS scanf calls for read, in order, an int "i", an char "c" and a float "f".
Input:
0 2.3
At this point: i=0 c=' ' f=2.3
Why? Well, at int reading all its normal BUT at char reading, happen something of unexpected. It read the space ' ' char. This because reading a char DONT consume previous whitespace (usually) then in "c" will be putted the space char. At end, when scanf will read the float "f", all its ok because input is constituted by " 2.3" and because int/float/double consume previous whitespaces, "f" will be readed correctly like 2.3. Same example, different input.
Input (note the multiple space to begin):
d 10
At this point: i=? (no read) c='d' f=10.0
Why? At start scanf TRY to read an int, and because of this, it consume all starting whitespace chars. At first not-whitespace char, try to read it like an int BUT it cannot convert "c" string (remember that scanf try to read an int from a digit while a whitespace or an invalid char in encountered) to int then read will fails. Take note that now input is "d 10" because trailing whitespaces are consumed but "d" was ONLY TESTED for check if it was convertable to int, then it keep in input. After this, second scanf will read a char and because THERE ARE NOT whitespaces at start (consumed by previous scanf on int) 'd' char is readed and putted in "c" var. Next, another call to scanf, will try to read an float. Here we have to think that input is now " 10" (note the spaces) BUT because scanf will try to read an float, it consume all starting whitespace (input="10") then try to read the input in "f" and because "10" is convertible to float, "f" will have the 10.0 value.
+ 1
..... CONTINUE....
One thing that you have to know is that you can consume whitespaces when you read other data ALSO by preponing a space in format string like:
// note the first space char in formatting string
scanf(" %c", &c);
Input:
f
Now "c" will contain the 'f' char. In practice putting a space into format string, will instruct scanf to consume any whitespace char (if any) before try to read the value (remember only that with int and float is not necessary)
I hope that you have understood
+ 1
wow you're awesome I understood and when I input for exp 4 6 2y without whitespace it really is reading it thanks for the care you're the best
0
maybe you didn't understand.
when I enter a letter it's just start run the program endlessly looping without even giving me the chance insert new number or letter it just running wild
2. so I didn't understand why does it skip scanf. What do I need to wrote and fix for it to give me the opportunity insert character value to the 'answer' variable
0
https://code.sololearn.com/c4HbjWB4unKQ/#c
like this one.
why does the scanf("%c",%answer); in the end of the program got skipped and than it just ending the program although its need to keep looping
0
https://code.sololearn.com/c4HbjWB4unKQ/?ref=app
is this code is what you meant?
0
interesting that if I make it %s instead of %c in the scanf function of "answer" variable in the end it just start infinite loop till it run of time
0
ariel Thank you but you are exageratting 😊. Anyway, you are welcome 👍👍👍