Introduce float into a program that deals only with ints

I have a program, but when I enter floating point numbers whenever the program asks for data, the program suddenly skips a step and moves to the final output. The program below:

#include <stdio.h> #include <stdlib.h> int main() { int a,b,c; int i; printf("Please enter a number: "); scanf("%d", &a); printf("Please enter a number: "); scanf("%d", &b); c = 0; for(i=0; i < b; i++) { c = c + a; } printf("%dx %d = %d\n", a, b, c); return 0; } 

When I enter int for a and float for b , the program will output the product as expected if the numbers after the decimal point for b are truncated. However, when I enter a float for a , the program does not accept the value for the second number b and instead skips this step and displays the integer version ax -858993460 = 0 .

For instance:

a = int, b = float

Enter the number: 3
Enter the number: 5.6
3 x 5 = 15

a = float, b = skipped

Enter the number 3.9
Enter the number: 3 x -858993460 = 0

All the flaws in the code are intentional, but I just wanted to know why it behaves the way I explained above. I know this because you are trying to enter a float in a signed integer, but I'm not sure what exactly makes it skip the second scanf("%d", &b) . Can anyone explain why this is happening?

Thanks.

+4
source share
3 answers

It seems like scanf() reads your "3" in the second case and ignores "9".

Then, when the second scanf() is called, there is text (".9") in the input buffer.

I can’t say exactly what he is doing with ".9". He may have found a point and simply interrupted there, and b is not initialized. It should be simple to determine what happens after going through the debugger.

But, basically, not all input is processed by the first call to scanf() and by trying to read the second call. And why is he not waiting for you to enter any data for the second call.

+2
source

The console entry is buffered by line; when you enter 3.9 into the %d format specifier, only 3 is consumed, the rest of the data remains buffered, so the second scanf () call tries to convert it according to its specifier, it finds '.' and aborts the conversion leaving b undefined.

scanf() will continue to fail until there is '\n' at the end of the input. You can do it this way:

  printf("Please enter a number: "); scanf("%d", &a); while( getchar() != '\n' ) { /* do nothing */ } printf("Please enter a number: "); scanf("%d", &b); while( getchar() != '\n' ) { /* do nothing */ } 

Please note that if the format specifier is %c , flash code modification is required, because the converted character may already be '\n' :

 scanf("%c", &c); while( c != '\n' && getchar() != '\n' ) { /* do nothing */ } 
+2
source

If the next character to be read cannot be converted to the current format, as indicated by the format specifier, scanf stops scanning and saves the current field, and it moves to the next input field (if any).

And this particular character is considered unread and is used as the first character of the next input field or any subsequent read operation.

In the above example, it scans 3 and then cannot resolve . format specifier "%d" . Therefore, it saves 3 in the variable a , leaving .9 unread. The control, when it moves to the next scanf statement, validates . but again, since he cannot resolve . format specifier "%d" , it skips the input scan for this field.

Now that the variable b not been assigned, it contains some garbage value. And any arithmetic operation with garbage values ​​results in garbage values.

+1
source

All Articles