具有无限循环和零钱柜台 c 的收银机

cash register with infinite loop and change counter c

我已尽我所能完成程序中的所有事情,但我无法让它告诉我适当的零钱数量,它对所有硬币价值都保持为 0。

另外,当输入的项目数量为 0 时,我将如何打破无限循环,

#include <stdio.h>
#include <math.h>

int main(void)
{
    //variables used
    int penny = 0;
    int nickel = 0;
    int dime = 0;
    int quarter = 0;
    int items = 0.0;
    double paid= 0.0;
    double total = 0.0;
    double price =0.0;
    double change = 0.0;
    int counter =0;

    for (;;)
    {
        printf("Please enter the number of grocery items:");
        scanf("%d", &items);

        for (counter = 1; counter <= items; counter++)
        {
            printf("Please enter the price for item #%d:", counter);
            scanf("%f", &price);

            total += price;

        }

        printf("Please enter amount payed:");
        scanf("%f", &paid);

        change = paid - total;

        while (change > 0)
        {
            if (change >= .25)
            {
                change = change - .25;
                quarter = quarter + 1;
            }
            else if (change >= .1)
            {
                change = change - .1;
                dime = dime + 1;
            }
            else if (change >= .05)
            {
                change = change - .05;
                nickel = nickel + 1;
            }
            else if (change >= .01)
            {
                change = change - .01;
                penny = penny + 1;
            }
        }

        printf("this is your change %d quarters, %d dimes, %d nickels, %d pennies.\n", quarter, dime, nickel, penny);



    }
}

您的 scanf() 说明符错误,请使用 double 中的 "%lf" 或者更好,将所有 double 变量更改为 float.

使用浮点数而不是双精度数。

在检查项目是否 == 0 后跳出循环。

for (;;)  {
    printf("Please enter the number of grocery items:");
    scanf("%d", &items);

    if (items == 0) {
        break;
    }

    for (counter = 1; counter <= items; counter++) {
        printf("Please enter the price for item #%d:", counter);
        scanf("%f", &price);
        total += price;
    }
    etc...
}