gpt4 book ai didi

CS50 Pset1 现金错误 "expected identifier or ' ('"含义?

转载 作者:行者123 更新时间:2023-11-30 19:28:20 25 4
gpt4 key购买 nike

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

int main(void)
{
{
float dollars;
// prompt user for "0.00" value
do
{
dollars = get_float("Change owed: ");
}
while(dollars <= 0);
}
// print amount of coins used for change
printf("%f\n", get_change(dollars));

int get_change(float dollars);

{
//calculate which coins will be used
int cents = round(dollars * 100);
int coins = 0;
int denom[] = {25, 10, 5, 1};

for (int i = 0; i < 4; i++);
{
coins += cents / denom[i];
cents = cents % denom[i];
}
return coins;
}
}

在 CS50 中执行 Pset1,我完全不明白为什么我的代码不起作用。出现语法错误

cash.c:6:1: error: expected identifier or '('

最佳答案

看起来您将一个函数放入另一个函数中。我无权访问头文件,但我认为您需要的更像是以下内容。

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

int main(void) {

float dollars;
// prompt user for "0.00" value
do {
dollars = get_float("Change owed: ");
} while (dollars <= 0);

// print amount of coins used for change
printf("%f\n",get_change(dollars));
return 0;
}

int get_change(float dollars) {
//calculate which coins will be used
int cents = round(dollars * 100);
int coins = 0;
int denom[] = {25, 10, 5, 1};

for (int i = 0; i < 4; i++);
{
coins += cents / denom[i];
cents = cents % denom[i];
}
return coins;
}

关于CS50 Pset1 现金错误 "expected identifier or ' ('"含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165283/

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