CS50 Pset1现金错误“期望的标识符或'('”的含义?

时间:2019-01-13 01:03:26

标签: c cs50

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

2 个答案:

答案 0 :(得分:0)

好像您将一个函数放在另一个函数中。我没有访问头文件的权限,但我认为您需要的是以下内容。

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

答案 1 :(得分:0)

您应在此行中删除[2 4 6 8]

;

在这里:

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

int get_change(float dollars); 移动到文件的开头或使用函数声明。