Coding a Simple Game

So I have to code a simple game.
1. The "random numbers" (couch, pocket and bed) are all the same.
2. I keep getting this "else without previous if" error message.

Please, please help.

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

using namespace std;

//Function Prototypes
int getRandomCouch ();
int getRandomBed();
int getRandomPocket ();

int main()
{//Declare Variables:

int look = 0;
int numPlaces =1;
int totalBucks = 0;
int randomCouch= getRandomCouch ();
int randomBed= getRandomBed ();
int randomPocket = getRandomPocket ();

//Game Intro:
cout << "Hey! My name's Andrey. I want to go buy some donuts, but I don't have any money. At the grocery store, a box of donuts costs 13 bucks! So here's my situation: I’ve got a few places I can look for some cash. Under my bed, in the couch, and in my pocket. If I can find 13 bucks, we can behold the glory of maple bars, strudels and even the jelly filled kind! Sigh. Please help me look. I’m hungry, and I’m sure you are too. I’ll share if you help me look." << endl;

//Look For Money
while (numPlaces <4)
{cout << "Where should we look? Press:" << endl;
cout << "1 to scrounge through the couch" << endl;
cout << "2 to look under the bed" << endl;
cout << "3 to look in my pockets" << endl;
cin >> look;
if (look == 1)
{
cout << "Awesome! We found " << randomCouch << " bucks!" << endl;
numPlaces += 1;

}
else if (look == 2)
{
cout << "Awesome! We found " << randomBed << " bucks!" << endl;
numPlaces += 1;
}

else if (look == 3)
{
cout << "Awesome! We found " << randomPocket << " bucks!" << endl;
numPlaces += 1;
}

} //endwhile

//Determine Win or Loss:
cout << "Let's add it up!" << endl;
totalBucks = randomCouch + randomBed + randomPocket;

if (totalBucks >= 13);
{

cout << "We found: " << totalBucks << " bucks."<< endl;
cout << "Hooray! Let's get a sugar rush!" << endl;
}
else;
{
cout << "We found: " << totalBucks << " bucks."<< endl;
cout << "Dang it... looks like we have to starve." << endl;
cout << "Let's look again?" << endl;
}
return 0;
}
int getRandomCouch ()
{
srand(time(0));
int randomCouch = 1 + rand() % 7 + 1;
return randomCouch;

}

int getRandomBed()
{
srand (time(0));
int randomBed = 1 + rand() % 7 + 1;
return randomBed;
}

int getRandomPocket ()
{
srand (time(0));
int randomPocket = 1 + rand () % 7 + 1;
return randomPocket;}

1. You're reseeding the random number generator with the same number every time you try to get a random number. Take the line srand (time(0)); out of your functions. Instead, write it once at the beginning of main.

2. In this if block, you have semicolons at the end of the 'if' condition and after 'else'. Remove these semicolons.
1
2
3
4
5
6
7
8
9
10
11
12
if (totalBucks >= 13); //shouldn't have a semicolon here
{

cout << "We found: " << totalBucks << " bucks."<< endl;
cout << "Hooray! Let's get a sugar rush!" << endl;
}
else; //shouldn't have a semicolon here
{
cout << "We found: " << totalBucks << " bucks."<< endl;
cout << "Dang it... looks like we have to starve." << endl;
cout << "Let's look again?" << endl;
}
Last edited on
So I fixed it but it keeps telling me that it expected a primary expression before int look.
And expected } before int somewhere.




while (numPlaces <4)
{cout << "Where should we look? Press:" << endl;
cout << "1 to scrounge through the couch" << endl;
cout << "2 to look under the bed" << endl;
cout << "3 to look in my pockets" << endl;
cin >> look;
if (int look == 1)
{
cout << "Awesome! We found " << randomCouch << " bucks!" << endl;
numPlaces += 1;

}
else if (look == 2)
{
cout << "Awesome! We found " << randomBed << " bucks!" << endl;
numPlaces += 1;
}

else if (look == 3)
{
cout << "Awesome! We found " << randomPocket << " bucks!" << endl;
numPlaces += 1;
}
Ye lol you put the data type "int" in your first if statement.
Delete the "int."
Topic archived. No new replies allowed.