gpt4 book ai didi

CS50: "expected prompt for input, not exit code of 0"不太确定我在这里做错了什么

转载 作者:行者123 更新时间:2023-11-30 15:03:54 24 4
gpt4 key购买 nike

我只是想知道我在这里做错了什么,因为当程序执行时,它运行得很好。我是编程新手,非常迷失。任何建议都会很棒!

#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
printf("Days in Month:\n");
int days = GetInt();
for (; days < 28 || days > 31; days = GetInt())
{
printf("No month has that amount of days! Please try again.\n");
}
printf("Pennies on first day:\n");
int pennies = GetInt();
for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}
if (pennies == 1)
printf("$%.2f",pow ((pennies + 1), (days - 1)));
else
printf("$%.2f",pow (pennies, days));
}

检查 50 yield :

~/workspace/chapter1/ $ check50 1617.chapter1.pennies pennies.c
:) pennies.c exists
:) pennies.c compiles
:( 28 days, 1 penny on first day
\ expected output, but not "$134217728.00"
:( 31 days, 1 penny on first day
\ expected output, but not "$1073741824.00"
:( 29 days, 2 pennies on first day
\ expected output, but not "$536870912.00"
:( 30 days, 30 pennies on first day
\ expected output, but not "$20589113209464899002378237447530552256..."
:) rejects days not 28, 29, 30, or 31
:( rejects non-positive pennies
\ expected prompt for input, not exit code of 0
:) rejects a non-numeric days input of "foo"
:) rejects a non-numeric penny input of "foo"
:) rejects a non-numeric days input of ""
:) rejects a non-numeric penny input of ""

正如您所看到的,它表示它正在接受负输入,并且没有产生正确数量的资金。这是 check50 中的错误,还是我做错了什么?

最佳答案

假设是this assignment ,你的数学错了。

如果您的初始硬币数量在一个月内每天翻倍,就会发生这种情况。如果您从 5 便士开始,则意味着 5 然后 10 然后 20 然后 40... 5 * 2 * 2 * 2 ... 或 5 * 2N 其中 N是天数。

但是你有pow(便士,天)。如果便士 = 5天数 = 30,则为 530,这是完全不同且非常大的东西。

您需要的是便士 * pow(2, 天)。这就是便士的数量,因此除以 100 即可得到美元金额。

这不是 100% 正确,但由于这是一个练习,我将其余的细节留给您。

<小时/>

关于“exit 0”问题,是这样的:

for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}

您允许 0 便士。你应该拒绝这一点。

If the user does not input a positive integer for the first day’s number of pennies, the program should prompt the user to retry.

至于为什么说没有收到提示,它给了你 0 便士,预计会再次提示。编写检查代码的人并没有想到看看它是否有输出。

关于CS50: "expected prompt for input, not exit code of 0"不太确定我在这里做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40498842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com