c - 为什么我的计数器不工作?

标签 c cs50

我应该获得用户输入,一个 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/

相关文章:

c - 在 xCode 中读取 C 错误

c - 这应该是 int,那我们为什么要使用 %s?

c - "Vigenere": String lowercasing program is appending character for some inputs

c - "Program received signal SIGSEGV , Segmentation fault” 尝试使用递归获取所有3字符组合的关键字时

CS50 - 卡在马里奥金字塔里

c - 在同一个 C 程序中同时使用 putchar 和 printf ?

c# - UINT8 中的自定义符号 - 如何转换为 C#?

c - 将二维数组传递给函数

C 合并排序段错误

cs50 pset3排序函数