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

标签 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: 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/

相关文章:

c - C 中的 UDP 字符串解析问题

c - 为什么通过直接函数调用替换变量时会得到不同的结果?

c - 我不知道为什么会收到段错误错误

Cs50 贪婪而不使用 <cs50.h>

C:二维数组的嵌套循环给出了意想不到的结果

c - .c 和 .h 文件扩展名对 C 意味着什么?

c - 选择语句和针对 NULL 的检查

c - 如何获取此字符串的信息?

c - 声明原子指针与指向原子的指针

c - 错误: passing 'float' to parameter of incompatible type 'const char *'