我正在执行 cs50 pset 1 cash,每当我输入 运行 这段代码时,终端就会变得空白

I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank

我正在执行 cs50 pset 1 cash,每当我输入 运行 此代码时,每次输入时终端都会变得空白。我写了一个数字,但它不接受。 我相信我的 get_cents() 函数有问题,但我不明白它是什么。

#include <cs50.h>
#include <stdio.h>

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);


int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters + dimes + nickels + pennies;

// Print total number of coins to give the customer
printf("%i\n", coins);
}

int get_cents(void)
{
int n;
do
{
    n = get_int("Money Owed: ");
}
while (n <= 0);
return n;
}


int calculate_quarters(int cents)
{
int i = 0;
if (cents >= 25)
{
    do
    {
        i += 1;
    }
    while (cents >= 25);
    return i;
}
return 0;
}

int calculate_dimes(int cents)
{
int i = 0;
if (cents >= 10)
{
    do
    {
        i += 1;
    }
    while (cents >= 10);
    return i;
}
return 0;
}

int calculate_nickels(int cents)
{
int i = 0;
if (cents >= 5)
{
    do
    {
        i += 1;
    }
    while (cents >= 5);
    return i;
}
return 0;
}

int calculate_pennies(int cents)
{
if (cents >= 1)
{
    return cents;
}
return 0;
}

这发生在我将 return i 行添加到所有函数时,我不知道该怎么做。

您所有的 calculate_* 函数都而不是 减少 cents 。它们是无限循环。

但是,所有这些功能都只是重复减法的除法。怎么样(例如):

int
calculate_quarters(int cents)
{

    return cents / 25;
}

你必须这样使用函数吗?使用 table 个面额(例如)int denom[] = { 25, 10, 5, 1 }; 并循环使用它可能会更好。


这是重构后的版本。注释为:

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

#define COUNTOF(_arr)       (sizeof(_arr) / sizeof(_arr[0]))

const int denoms[] = { 25, 10, 5, 1 };
const char *names[] = { "quarters", "dimes", "nickels", "pennies" };

int
get_cents(void)
{
    int n;

    do {
        n = get_int("Money Owed: ");
    } while (n <= 0);

    return n;
}

int
main(int argc,char **argv)
{

    --argc;
    ++argv;

    int cents = get_cents();
    printf("%d cents\n",cents);

    int coins = 0;
    for (int idx = 0;  idx < COUNTOF(denoms);  ++idx) {
        int denom = denoms[idx];

        // is amount remaining larger than the denomination of the current coin?
        if (cents >= denom) {
            // get number of the current denomination to dispense
            int count = cents / denom;

            // remember total number of coins
            coins += count;

            // show how many of this coin to dispense
            printf("%d %s\n",count,names[idx]);

            // reduce total number of cents
            cents %= denom;
        }
    }

    // show the number of coins
    printf("%d coins\n",coins);

    return 0;
}

这是一些程序输出:

Money Owed: 137
137 cents
5 quarters
1 dimes
2 pennies
8 coins