pdenlinger
pdenlinger

Reputation: 3917

Expected ';' at end of declaration

Am getting this error message for the following code, but it doesn't make sense at all. Can you please check this code and tell what's wrong? Have commented the code on the relevant line.

#import <Foundation/Foundation.h>

typedef struct {
    float exchangeRate;
    double budget;
    double euroTransaction;
} budget;

int main(int argc, const char * argv[])
{
    budget vacationBudget;

    void spendDollars (double dollars) {  //Expected ';' at end of declaration
        vacationBudget.budget -= 100;
    }

    void chargeEuros(double euros) {
        vacationBudget.euroTransaction = euros*vacationBudget.exchangeRate;
        vacationBudget.budget -= vacationBudget.euroTransaction;
    }

    vacationBudget.exchangeRate = 1.2500;
    vacationBudget.budget = 1000.00;
    double numberDollars = 100;
    double numberEuros = 100;

    spendDollars(numberDollars);
    NSLog(@"Converting %.2f US dollars into euros leaves $%.2f", numberDollars, vacationBudget.budget);



    NSLog(@"Charging %.2f euros leaves $%.2f", numberEuros, vacationBudget.budget);

    return 0;
}

Upvotes: 0

Views: 5692

Answers (5)

Heinrisch
Heinrisch

Reputation: 5935

Funcitions can't be inside main. Change to:

#import <Foundation/Foundation.h>

    typedef struct {
        float exchangeRate;
        double budget;
        double euroTransaction;
    } budget;

budget vacationBudget;

void spendDollars (double dollars) {  //Expected ';' at end of declaration
            vacationBudget.budget -= 100;
    }

    void chargeEuros(double euros) {
            vacationBudget.euroTransaction = euros*vacationBudget.exchangeRate;
            vacationBudget.budget -= vacationBudget.euroTransaction;
    }

    int main(int argc, const char * argv[])
    {

        vacationBudget.exchangeRate = 1.2500;
        vacationBudget.budget = 1000.00;
        double numberDollars = 100;
        double numberEuros = 100;

        spendDollars(numberDollars);
        NSLog(@"Converting %.2f US dollars into euros leaves $%.2f", numberDollars, vacationBudget.budget);



        NSLog(@"Charging %.2f euros leaves $%.2f", numberEuros, vacationBudget.budget);

        return 0;
    }

Upvotes: 2

sidyll
sidyll

Reputation: 59287

That's because you're defining a function (spendDollars) inside another function (main). Nested functions are not allowed, they're always "global" in that sense.

However, you're allowed to declare a function inside another, which then acts as a clue to the compiler: that function is declared elsewhere but now it knows the parameters and return type (which defaults to int if you use an un-declared function).

So, then the compiler sees the function syntax inside another function, it expects it to be a declaration, just like function signatures. And then it sees the opening brace and generates an error, as the declaration ended without a semicolon before the following block.

You need to move the functions spendDollars and chargeEuros to outside main, and for that reason you have to pass a reference to the local variable you want to modify. These should work:

void spendDollars (budget *b, double dollars)
{
    b->budget -= 100;
}

void chargeEuros(budget *b, double euros) {
    b->euroTransaction = euros*vacationBudget.exchangeRate;
    b->budget -= vacationBudget.euroTransaction;
}

Note that the -> operator will work directly on the pointer, just like if you had used

(*b).budget -= 100;

To dereference the pointer and operate on the struct. Call your new function as

spendDollars(&vacationBudget, numberDollars);

Upvotes: 1

Justin
Justin

Reputation: 2999

Your functions spendDollars and chargeEuros can't be in the main.

But its not true that you can't create methods in methods. Sinds iOS 4.0 you can create Blocks.

For more information follow this tutorial about Blocks

Upvotes: 0

ncremins
ncremins

Reputation: 9200

Not a great idea to have a function within a function (as in it won't compile - i.e. what you're seeing - and it doesn't make logical sense), move spendDollars and changeEuros outside of main and pass vacationBudget as a parameter

Upvotes: 1

thelazydeveloper
thelazydeveloper

Reputation: 3878

Move the spendDollars and chargeEuros functions outside of the main function.

Upvotes: 3

Related Questions