繁体   English   中英

CS50 Pset1 Cash 错误“预期标识符或‘(’”是什么意思?

[英]CS50 Pset1 Cash error "expected identifier or '('" meaning?

#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: 错误:预期标识符或 '('

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

#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;
}

你应该删除; 在这一行:

for (int i = 0; i < 4; i++);

和这里:

int get_change(float dollars);

get_change移至文件的第一个或使用函数声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM