将float输入到仅处理int的程序中 [英] Inputting float into a program that only deals with ints

查看:134
本文介绍了将float输入到仅处理int的程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,但是当程序要求输入时输入浮点数时,程序会突然跳过一步并移动到最终输出。程序如下:

  #include< stdio.h> 
#include< stdlib.h>

int main()
{
int a,b,c;
int i;

printf(请输入一个数字:);
scanf(%d,& a);
printf(请输入一个数字:);
scanf(%d,& b);

c = 0; (i = 0; i< b; i ++)

{
c = c + a;
}

printf(%d x%d =%d\\\
,a,b,c);

return 0;
}

当我输入一个int $ a ,以及 b 的浮点数,如果 b 被截断。但是当我为 a 输入一个浮点数时,程序不会使用第二个数字 b 的值跳过步骤并输出整数版本的 ax -858993460 = 0



例如:



a = int,b = float


请输入一个数字:3

请输入一个数字:5.6

3 x 5 = 15


a = float,b =跳过


请输入一个数字3.9

请输入一个数字:3 x -858993460 = 0


代码中的所有缺陷都是故意的,但我只想知道为什么它的行为方式如上所述。我知道这是因为尝试将一个浮点数输入一个有符号整数,但我不确定是什么导致它跳过第二个 scanf(%d,& b) 。任何人都可以解释为什么会发生这种情况?



谢谢。

解决方案

看起来像 scanf()在第二种情况下读取3,忽略9。



然后当调用第二个 scanf()时,输入缓冲区中已经有文本(.9)。



我不能准确地告诉它在.9中正在做什么。它可能已经找到了点,刚刚中止了b未初始化。通过调试器来确定发生的情况应该是一件简单的事情。



但是,基本上,并不是所有的输入都是由第一次调用 scanf()所以这是第二个调用尝试读取的。这就是为什么它不等待你输入第二个电话的任何数据。


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 enter 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.

这篇关于将float输入到仅处理int的程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆