我正在做CS50 pset 1现金,每当我运行此代码时,终端都会变得空白
我正在执行CS50 PSET 1现金,每当我运行此代码时,终端都会在我按下的每个输入时都变得空白。我写了一个号码,但没有接受。 我相信我的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;
}
当我添加返回我将所有功能添加到所有功能时,我不知道该怎么办。
I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank with every enter i press. i write a number and it does not take it.
i believe there is a problem with my get_cents() function but i cannot understand what it is.
#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;
}
This happened when I added the return i line to all the functions, and i dont know what to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有
计算_*
函数 do dos 降低Cents
应有。它们是无限的循环。但是,所有这些功能只是通过重复减法而分裂。 (例如):
您是否需要以这种方式使用功能?您可能会使用一张教派表(例如)
int denom [] = {25,10,5,1};
and循环。这是重构版本。它已注释:
这是一些程序输出:
All your
calculate_*
functions do not decrementcents
as they should. They are infinite loops.But, all these functions are just division by repetitive subtraction. How about (e.g.):
Are you required to use functions in this way? You'd probably be better off with a table of denominations (e.g.)
int denom[] = { 25, 10, 5, 1 };
and loop on that.Here is a refactored version. It is annotated:
Here is some program output: