Question

I am trying to build a really simple first game with simulated dice rolls.

I am getting an error on line 49: if(rollResult>aiRollResult).

I'm sure it's just a really simple syntax error with all the if statements but I cannot figure out how to fix it or can I not call the airoll() function in the middle of my code like I did?

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

void airoll();

int main() {
    int b;
    srand(time(0));
    //random die roll
    int rollResult = 1+(rand()%6);

    switch (rollResult) //results options {
        case 1: {
            cout<<"Your Roll: 1"<<endl;
        }
        break;
        case 2: {
            cout<<"Your Roll: 2"<<endl;
        }
        break;
        case 3: {
            cout<<"Your Roll: 3"<<endl;
        }
        break;
        case 4: {
            cout<<"Your Roll: 4"<<endl;
        }
        break;
        case 5: {
            cout<<"Your Roll: 5"<<endl;
        }
        break;
        case 6: {
            cout<<"Your Roll: 6"<<endl;
        }
        break;
    }

    airoll();

    if(rollResult>aiRollResult) {
        cout<<"You win!"<<endl;
    }

    if (aiRollResult>rollResult) {
        cout<<"You lose!"<<endl;
    }

    if (rollResult==aiRollResult) {
        cout<<"It's a tie!"<<endl;
    }
}

void airoll() {
    int aiRollResult=1+(rand()%6);
    cout<<"AI roll: "<<aiRollResult<<endl;
}
Était-ce utile?

La solution

Your variable aiRollResult is not defined in the scope of the main() function. It only exists in the scope of the airoll() function. Change your airoll() to

int airoll()
{
    int aiRollResult=1+(rand()%6);
    cout<<"AI roll: "<<aiRollResult<<endl;
    return aiRollResult;
}

Notice the int return type.

Now you can get the result of the airoll() in the main function as follows:

int aiRollResult = airoll();

Which will call the airoll() function and then store the result in the variable. That should solve your problem. Learn more about functions here: http://www.cplusplus.com/doc/tutorial/functions/

Autres conseils

You've to declare and initialize a variable named aiRollResult which would be from the function airoll(). So change the function from returning void to returning int. Decouple the message display; show it in main.

This is really simple. The aiRollResult variable is a local variable which can't be seen outside of its function's brackets

To solve this you can declare a global variable and initialize it to the aiRollResult value

In your code, I see:

switch (rollResult) //results options {
    [.. bunch of case statements...]
}

airoll();

Notice that the opening { of the switch statement is hidden by a // comment.

But you still have the closing }.

I think you have mismatched braces.

The final brace, which you think is closing your switch statement, is actually closing main. Then the airoll(); is a function call outside any function, causing an error.

Others have described the scoping problem of aiRollResult, but I don't think that is the direct cause of the error you're describing.

(of course, if you had posted the exact error message in your original question, we could be more helpful)

The aiRollResult variable inside the function:

void airoll() {
    int aiRollResult=1+(rand()%6);
    cout<<"AI roll: "<<aiRollResult<<endl;
}

is a local variable to the function. It means that when the function has done running, the integer is deallocated. I'd suggest you to return the integer instead:

int airoll() {
    int aiRollResult=1+(rand()%6);
    cout<<"AI roll: "<<aiRollResult<<endl;
    return aiRollResult;
}

Now you can replace:

airoll();

with:

int aiRollResult = airoll();

and you are set.

I've also noticed that in the switch statements you are doing:

    case 1: {
        cout<<"Your Roll: 1"<<endl;
    }
    break;

The enclosing brackets are not required at all. With those brackets you are creating an inner block of code., which is only useful if you are declaring a variable within the case statement.

You can rewrite those lines as:

case 1: cout << "Your Roll: 1" << endl; break;

Finally, don't pollute the current namespace with:

using namespace std;

get used to use the std:: prefix or include what you really need with:

using std::cout;
using std::endl;
// ...

To sum up everything that's been said, but nobody has actually said all at once:

    switch (rollResult) //results options {

This is causing a mismatched brace. Put your brace before the comment, like this:

    switch (rollResult) { // results options

While this is technically correct, it sucks:

    case 1: {
        cout<<"Your Roll: 1"<<endl;
    }
    break;

Change it to this:

    case 1:
        cout<<"Your Roll: 1"<<endl;
        break;

This doesn't do anything useful for the if statements that follow:

    airoll();

Make it look like this:

    aiRollResult = airoll();

Of course, that means that you'll have to declare aiRollResult up above in your declarations section.

Finally, this function prints a value, but you can't use the value, because it immediately throws that value straight into the trash can:

    void airoll() {
        int aiRollResult=1+(rand()%6);
        cout<<"AI roll: "<<aiRollResult<<endl;
    }

Make it look like this:

    int airoll() {
        int aiRollResult = 1 + (rand() % 6);
        cout << "AI roll: " << aiRollResult << endl;
        return aiRollResult;
    }

Also, the comment about namespace pollution might be taken into account, but let's not get into a coding style fight, eh?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top