开发者

Inputting float into a program that only deals with ints

I have a program, but when I input float numbers whenever the program asks for inputs, the program abruptly skips a step and moves onto the end output. The program is 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("%d x %d = %d\n", a, b, c);

  return 0;
}

When I input an int for a, and a float for b, the program will output the product as expected if the numbers after the decimal point for b is truncated. However when I input a float for a, the program doesn't take the value for the second number b and instead skips that step and outputs the integer version of a x -858993460 = 0.

For example:

a = int, b = float

Please enter a number: 3

Please enter a number: 5.6

3 x 5 = 15

a = float, b = skipped

Please enter a number 3.9

Please ent开发者_高级运维er a number: 3 x -858993460 = 0

All the flaws in the code are deliberate, but I just wanted to know why it behaves the way I explained above. I know it's because of something to do with trying to input a float into a signed integer but I'm not sure what exactly is causing it to skip the second scanf("%d", &b). Can anyone explain why this happens?

Thanks.


It looks like scanf() is reading your "3" in the second case, and ignoring the "9".

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

I can't tell exactly what it's doing with the ".9". It may have found the dot and just aborted there with b uninitialized. It should be a simple matter to determine what is happening by stepping through with the debugger.

But, basically, not all the input is being processed by the first call to scanf() and so that's what the second call is trying to read. And that's why it's not waiting for you to input any data for the second call.


Console input is line buffered; when you enter 3.9 into a %d format specifier, only the 3 is consumed, the remaining data remains buffered, so the second scanf() call attempts to convert it according to its specifier, it finds a '.' and aborts the conversion leaving b undefined.

scanf() will continue to "fall-through" until the '\n' at the end of the input data is consumed. You can do this thus:

  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 */ }

Note that if the format specifier is %c, a modification of the "flush" code is required, because the converted character may already be '\n' :

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


If the next character that is to be read cannot be converted under the current format as specified by the Format Specifier, scanf stops scanning and storing the current field and it moves to the next input field (if any).

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

In the example given above, it is scanning 3 and then cannot resolve . to the format specifier "%d". Hence it stores 3 in variable a leaving .9 as unread. The control when passes to the next scanf statement, it scans ., but again as it cannot resolve . to format specifier "%d", it skips the input scanning for that field.

Now as variable b was not assigned, it contains some garbage value. And any arithmetic operation with garbage values result into garbage values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜