C:无法使用模数运算符将零钱分解为面额,存储到数组中并将输出打印给用户

C: Unable to use modulus operators to break down change into denominations, storing into an array and print the output to the user

我正在尝试编写一个程序,其中:

目的是用一个数组来保存硬币的值,所以我"can write a general purpose change calculator that can be used for any coinage by changing the contents of the array"。

这是我的代码:

void vendingMachine()
{

    // Declarations
    #define ARRAY_LENGTH 6

    int itemCost;
    int amountEntered;
    int fifty, twenty, ten, five, two, one; 
    int remainder;

    // User input

    printf("Please enter the cost of the item in pence: ");
    scanf_s("%d", &itemCost);
    while (itemCost <= 0 || itemCost > 99)
    {
        printf("You've entered an invalid amount. Please enter an amount between 1p and 99p: ");
        scanf_s("%d", &itemCost);
    }

    printf("Please enter the amount entered into the machine in pence: ");
    scanf_s("%d", &amountEntered);
    while (amountEntered <= 0 || amountEntered > 100)
    {
        printf("You've entered an invalid amount. Please enter an amount between 1p and 100p: ");
        scanf_s("%d", &amountEntered);
    }

    while (amountEntered < itemCost)
    {
        printf("You've entered an invalid amount. Please enter an amount equal to or higher than the cost of the item: ");
        scanf_s("%d", &amountEntered);
    }

    // Program to determine if the customer is owed any change and, if so, how much is owed

    if (amountEntered == itemCost)
    {
        printf("No change is owed to the customer");
    }
    else if (amountEntered > itemCost)
    {
        int change = amountEntered - itemCost;
        printf("The amount of change owed to the customer is: %d pence, broken down as follows: \n", change);

        fifty = change / 50;
        remainder = change % 50;
        twenty = remainder / 20;
        remainder = remainder % 20;
        ten = remainder / 10;
        remainder = remainder % 10;
        five = remainder / 5;
        remainder = remainder % 5;
        two = remainder / 2;
        remainder = remainder % 2;
        one = remainder;

         // Program to store the change in an array

        int count[ARRAY_LENGTH];
        count[0] = fifty;
        count[1] = twenty;
        count[2] = ten;
        count[3] = five;
        count[4] = two;
        count[5] = one;

        for (int i = 0; i < ARRAY_LENGTH; i++)
        {
             count[i] = 0;
        }

        for (int i = 0; i < ARRAY_LENGTH; i++)
        {
            printf("The number of %d coins is: %d\n", //I don't know what to do here);
        }
    }
}

也将硬币类型存储在一个数组中,例如

const int coins[ARRAY_LENGTH] = { 50, 20, 10, 5, 2, 1 };

然后您可以在循环中轻松引用它们:

printf("The number of %d coins is: %d\n", coins[i], count[i]);

这还允许您在循环中执行模计算。

我不确定你想在这里实现什么:

以下(您的)代码将 count 的值设置为从索引 0 到索引 5,从 fiftyone ..

int count[ARRAY_LENGTH];
count[0] = fifty;
count[1] = twenty;
count[2] = ten;
count[3] = five;
count[4] = two;
count[5] = one;

然后在这里,您将在 for 循环中用 0 覆盖那些。

for (int i = 0; i < ARRAY_LENGTH; i++)
{
     count[i] = 0;
}

所以上面的循环不是必需的,或者至少不能放在之后你已经分配了fiftytwenty,[=22的值=]、fivetwoonecount 数组索引。

我猜你是想打印它们?你不必在这里使用循环:

// Doing it the newbie-way:

printf("The number of coins of 50 are: %d\n", count[0]);
printf("The number of coins of 20 are: %d\n", count[1]);
printf("The number of coins of 10 are: %d\n", count[2]);
printf("The number of coins of 5 are: %d\n", count[3]);
printf("The number of coins of 2 are: %d\n", count[4]);
printf("The number of coins of 1 are: %d\n", count[5]);