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.
#include <iostream>
#include <cstdlib>
#include <time.h>
usingnamespace 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'); {
}
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! :)
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?
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
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.