我应该获得用户输入,一个 float ,然后使用计数器跟踪可以从中减去 0.25、0.10、0.05 和 0.01 的次数。然后它应该打印计数。但是当我尝试运行代码时,它会获取用户输入,但是当我尝试任何数字时,都会显示:
greedy.c:18:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be >represented in type 'int'
请指出任何错误,这是我的代码:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float b;
float a;
int count = 0;
printf("How much change is owed? ");
a = GetFloat();
do
{
b = a - 0.25;
count++;
}
while(a>0.25);
do
{
b = a - 0.10;
count++;
}
while(a>0.10);
do
{
b= a - 0.05;
count++;
}
while(a>0.05);
do
{
b= a- 0.01;
count++;
}
while(a>0.01);
printf("%d coins\n", count);
}
最佳答案
尝试使用“a = a - 0.25;
”代替“b = a - 0.25;
”。其他减法也一样。变量 a
现在在您的循环中保持不变。
关于c - 为什么我的计数器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330396/