#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int main()
{
int balance = 20;
int bet;
int bet_amount;
cout << "This is a guessing game!" << endl;
cout << "You will input a bet, you will start with 20 coins" << endl;
cout << "Give us your starting bet amount:";
cin >> bet_amount;
if (bet_amount < 20) {
cout << "Enter a number 1-10:";
cin >> bet;
int x;
srand(time(0));
x = rand() % 10 + 1;
cout << "The outcome was:" << x << "." << "You entered:" << bet << "." << endl;
for (bet = x; balance > 0;) {
int increase(balance + bet_amount);
cout << "You guessed the right number, your bet amount will be added to your balance" << endl;
cout << "Your balance is now:" << increase << "." << endl;
break;
}
}
if (!bet = x); {
int decrease(bet_amount - balance);
cout << "Your balance is now:" << decrease << endl;
cout << "You did not guess the right number would you like to try again?" <<
"Enter 'y' or 'n'";
bool try_again = true;
while (try_again == true) {
int tryagain;
cin >> tryagain;
if (tryagain == 'y' && 'Y') {
tryagain = true;
}
elseif (tryagain == 'n' && 'N') {
tryagain = false;
}
return EXIT_SUCCESS;
}
}
}
C:\CodeBlocks Projects\Guessing game\main.cpp|45|error: expected unqualified-id before 'if'|
I keep getting this, i commented out the for loop to see if that was the problem and it wasn't. Though when i did that i got a error saying that x was not declared in that scope. So im guessing that is some sort of bug i will have to fix. Any tips on the problem would be great!
In addition to shadder's help, you also haven't declared what x is.
What's the difference between the bet, bet_amount and balance? Aren't they the same or similar? Once I ironed out the problem I managed to win some money on the first run. Pleas pay up :)
x was declared, it's just not with all the other integers.
Oh no it wasn't! In function 'int main()': 45:16: error: 'x' was not declared in this scope
What would the proper syntax be?
One that translates to what a betting system would actually check. You need to think about it. Forget about code and C++ what checks would you carry out if you were the one responsible for accepting or rejecting punters bets?
I edited my comment when I realised what I said was wrong. If you'd read my revision that might help. It would check if the numbers matched and if the numbers match it'd be accepted and vice versa. I was trying to put that in as a loop and I suppose I just couldn't manage to get the loops to work how I wanted. :P
I was talking about my comment not the code itself, I'm on my phone right now away from my computer. I'm sorry if I sounded like I was arguing, much apologies. Thanks for the help so far, I'll have to try it in the morning ! :)
No problem. Think about what the bet manager would do if I came up to place a bet of $45 and I had $100 in my account, or if I wanted to place a $100 bet with $45 in my account. And after a valid bet is processed how much is left in my account.
You'll be disappointed if somebody just gives you the answer.
Think scope. (Hint: maybe move a line)
Think if (Hint: write down the value of x as a number) ie what does x mean?
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace std;
int main() {
int x;
int balance = 20;
int bet;
int bet_amount;
int increase (balance + bet_amount);
int decrease (balance - bet_amount);
cout << "This is a guessing game!" << endl;
cout << "You will input a bet, you will start with 20 coins" << endl;
cout << "Give us your starting bet amount:";
cin >> bet_amount;
while (bet_amount <= balance) {
cout << "Enter a number 1-10:";
cin >> bet;
srand(time(0));
x = rand() % 10 + 1;
if (bet = x) {
cout << "You guessed the correct number!" << increase << endl;
increase = balance;
} elseif (bet != x); {
cout << "You guessed the wrong number!" << decrease << endl;
decrease = balance;
return 0;
}
}
}
when i type in the bet it dosent check against the x to see if there the same and it just continues to say that it was correct at increase and decrease the balance. The balance also dosent properly increase or decrease. Sorry for how noobish this is. Any help is appreciated.
But using (bet !== x) gives expected primary expression before '=' token. What's causing this? Also it not adding or subtracting correctly using the increase and decrease int...
I should have edited my comment, I tried it and realised that I was wrong. I'm still having the problem that it's not properly adding and subtracting .