Hi I'm writing a very simple horse betting game but I keep getting a syntax error at line 54 and I do not know why. In the tutorial it works fine, have I missed something out? Or am I doing something wrong? Cheers!
On either side of the && you need to have a full statement. That means that the right-hand-side of the condition on line 54 is similar to while (> 3);.
Change line 54 to: while (userInput < 0 && userInput > 3);
On line 80 to 84 I made it so it doesn't allow anyone to bet more than they have. It works except it takes away $1. Maybe the return -1? I can't think of another way...
#include <iostream>
#include <Windows.h>
#include <ctime>
usingnamespace std;
int main(void);
int race(int, int);
void race(void);
int menu(void);
int placeBets(int);
int money = 100;
int main()
{
srand(0);
int userInput;
cout << "Welcome to the races.\n";
while (userInput = menu())
{
switch (userInput)
{
case 1:
case 2:
case 3:
::money += race(placeBets(userInput), userInput);
break;
case 4:
race();
break;
}
}
}
int menu(void)
{
int userInput;
cout << "You currently have $" << money << ".\n";
do
{
cout << "Racing Menu\n";
cout << "***********\n";
cout << "1) Bet on first horse\n";
cout << "2) Bet on second horse \n";
cout << "3) Bet on third horse\n";
cout << "4) Watch don't bet\n";
cout << "0) Leave\n";
cin >> userInput;
}
while (userInput < 0 && userInput > 3);
return userInput;
}
int placeBets(int userInput)
{
int placeBets;
cout << "\nYou're betting on horse " << userInput << "\n";
cout << "How much would you like to bet on " << userInput;
cout << "\n$";
cin >> placeBets;
return placeBets;
}
void race(void)
{
race(0, 0);
}
int race(int money, int userInput)
{
if( userInput <= money) // If bet more than you have, returns to betting menu
{
cout << "You cannot bet more than you have, please try again\n";
return -1;
}
int racewinner = rand() % 3;
cout << "The horses are racing...\n";
Sleep(3000);
system("CLS");
cout << "Horse " << racewinner << " won!\n";
Sleep(2000);
if(racewinner = userInput)
{
cout << "You're horse won!\n";
cout << "You doubled your $!\n";
return 2 * money;
}
else
{
cout << "\n Unfortunatly you bet on " << userInput << " but horse " << racewinner << " won.\n";
cout << "\n You lost $ " << money;
system("CLS");
return -1 * money;
}
}