Guessing Game

Pages: 12
Im creating a quite simple guessing game, and i keep getting a error, im sure its something very stupid. Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 #include <iostream>
#include <cstdlib>
#include <time.h>

using namespace 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;

        }

        else if (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!
Hi,

line 45, the syntax is wrong (as the compiler said)

if (bet != x); {//wrong syntax before
Last edited on
closed account (48T7M4Gy)
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 :)
Last edited on
X is the rand() function, or am i wrong in thinking that? What would the proper syntax be?
Last edited on
closed account (48T7M4Gy)
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
Last edited on
closed account (48T7M4Gy)
If you'd read my revision that might help.

Advice is free, arguments are very expensive especially when I checked your code using the shell at the time and I note that the error is still there.

You'll have to think a little more about the test whether a bet is valid or not.

You still need to fix the scope problem.

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 ! :)
closed account (48T7M4Gy)
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?
I changed my code up a little bit, i just cant get these loops to interact right, ill post my code so you can see:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace 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;
 }  else if (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.
Last edited on
Hi,

if (bet = x)


When you compare, don't use assignment operator (=), use comparison operator (==)

if (bet == x)
Line 32 is setting bet equal to x. Use == for comparison.
@cire
I think you are a little late to the party :)
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...
Last edited on
Sorry for all these small questions, I'm terrible a debugging I'm trying to get better :P
@YvngSavage
Are you trying to make your program run incorrectly on purpose?

Noone ever said that what you did with != is wrong. You should at least realize that.
Last edited on
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 .
> I'm still having the problem that it's not properly adding and subtracting.

So assuming you manage to guess a number correctly, your balance will increase. And vice-versa.

If you guess a number correctly :
balance += bet_amount;

If you guess a number incorrectly :
balance -= bet_amount;

1
2
3
4
5
6
7
8
9
if (bet == x) 
{
    cout << "You guessed the correct number!" <<  (balance += bet_amount) << endl;
 }  else if (bet != x); 
{
  cout << "You guessed the wrong number!" << (balance -= bet_amount) << endl;
 
    return 0;
}
else if (bet != x); {}

Putting a semi-colon ( ; ) after an if-expression makes an if-statement totally meaningless.

else if (bet != x) {}
Thank you'll so very much! :)
Pages: 12