Search code examples
ccompiler-errorsidentifier

How do I fix this expected Identifier error in C?


I keep getting an “expected identifier or “(“ error but I can’t seem to find a way to solve this. My code is attached Screenshot of code

I’ve read around that it could be caused by a lack of function name / definition. I already have that, so i’m not sure what else to check as I am self learning using CS50

Code:

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

float half(float bill, float tax, int tip); //function declaration

int main(void)

    {
        float bill_amount = get_float("Bill before tax and tip: ");
        float tax_percent = get_float("Sale Tax Percent: ");
        int tip_percent = get_int("Tip percent: ");

        printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent)); //function call
    }


float half(float bill, float tax, int tip); //function definition
    {
        float result, plus_tax;
        float bill = bill_amount;
        float tax = tax_amount / 100;
        int tip = tip_percent / 100;
        plus_tax = bill + bill * tax;
        result = plus_tax + plus_tax * tip;
        return(result);
    }

Update: Thanks for the comments. I have updated my code as seen below.

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

float half(float bill, float tax, int tip); //function declaration

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent)); //function call
}


float half(float bill, float tax, int tip) //function definition
{
    float result, plus_tax;
    plus_tax = bill + bill * (tax / 100);
    result = (plus_tax + plus_tax * (tip / 100))/2;
    return(result);
}

Solution

  • The definition of float half(float bill, float tax, int tip); should not be followed by a semicolon. You end function prototype with semicolon on declaration, not on definition. Also, the bill_amount, tax_amount, tip_pourcent variables in half function should have been renamed to the function's argument matching names (ie bill, tax and tip)