Error

I suppose im overlooking something but i keep getting an error : expected '}' at end of input. Am i overseeing something that should be obvious? I checked and didnt notice and } missing so im not sure. Any help would be great.
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
  #include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main() {

int x;
int balance = 20;
int bet;
int bet_amount;
int choice;

cout << "This is a guessing game!" << endl;
cout << "You will input a bet, you will start with 20 coins" << endl;
do {
        cout << "Give us your starting bet amount:";

cin >> bet_amount;

if (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!" << (balance += bet_amount) << endl;

 }   if  (bet != x) {

  cout << "You guessed the wrong number!" <<  (balance -=bet_amount) << endl;
  cout << "Would you like to play again type 'y' or 'n'?";

  cin >> choice;  }

  if (choice = 'n') {

    return EXIT_SUCCESS;

  }

 }  while (choice != 'n'); {



}




Take off the last '{' on line 48.
Unrelated: line 28, srand() is called repeatedly inside the do-while loop. This is not a good idea, it is best to call srand just once at the start of the program.
Why do you say that? How does it affect my program?

Edit: I've fixed my program and fixed other issues besides the one I posted about will post it all later, and hopefully get some help on what I might need to work on I'm the future! :)
Last edited on
Worst-case scenario, the same seed will be used each time, meaning the same so-called random number generated each time. Since there is a user input via cin, the time delay will probably give some slight change in the seed value, but it still decreases the randomness.
> Why do you say that?
'Cause you don't have to make the same code run for multiple times for nothing, while the nature of the function tells you that only one call to it is more than sufficient. You don't want to waste computer resources. Thus, it is always advisable that you call the function at the very beginning of the function main() (only once). Do you know how to do that?
Last edited on
Ahh, I see. So your suggesting I just declare it where I have all the variables outside the main loop? If so I will do that, I never realised that. Much obliged
example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    for (int i=0; i<10; ++i)
    {
        srand(time(0));
        int x = rand() % 10 + 1;
        cout << x << ' ';
    }   
}

typical output:
3 3 3 3 3 3 3 3 3 3



Corrected example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    for (int i=0; i<10; ++i)
    {
        int x = rand() % 10 + 1;
        cout << x << ' ';
    }   
}

typical output:
9 8 4 1 3 1 1 5 4 6

By the way, this is nothing to do with 'wasting computer resources'. It is a matter of which algorithm to use for the required results.
But this is the function that is supposed to be called once. I would never put this function inside any for or while loop.
Never say never. There might be some case where the random number generator needs to be re-seeded in order to solve a particular problem. But that would be an advanced usage.
Okay, I'll change it and see if anything changes, as of right now it seems to run perfect though.
Topic archived. No new replies allowed.