CS50 Pset1 Cash error “expected identifier or '

2019-12-16 19:23发布

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

Doing Pset1 in CS50 and I'm completely lost as to why my code isn't working. Getting syntax error

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

标签: c cs50
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-12-16 20:11

You should remove ; in this line:

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

and here:

int get_change(float dollars);

move get_change to first of file or use function declarations.

查看更多
Melony?
3楼-- · 2019-12-16 20:12

It looks like you put one function inside another function. I don't have access to the header file, but I think what you need is more like the following.

#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;
}
查看更多
登录 后发表回答