### Guessing game c++

Hi i am new here,i am trying to learn c++. I am making a number guessing game, the game check for valid input, if the guess is in range,if the guess allready has been guessed,tells the player if the guess is higher or lower then the randomised number, and gratulate the plyer when the right number has been guessed, there is two other things i am hving troubble with, if the player plays again the number of guesses doesnt reset to zero and the randomised number doesnt change, those two things is what i got stucked on, i have tried to put the srand(time(0)); and int number = rand() % 100 + 1; in the while loop so that runs if the player desides to play again i have written a specified GenNumber function but that did mess upp the code so the structure and logic is important, kind reegards

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152`` `````` #include #include #include #include #include using namespace std; bool allreadyguessed(int guesses[], int guess) { for (int i = 0; i < 25; i++) { if (guesses[i] == guess) return true; } return false; } bool checkvalidguess() { if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(1000, '\n'); return false; } return true; } int main(void) { int index = 0; char answer = 0; int guesses[100] = { -1 }; int count = -1; int guess =0; char input; int mostAttempts = 0; int totalguesses = 0; string name; bool t = true; answer = count++; cout << "enter your name " << endl; cin >> name; cout << "welcome to this super awesome action game " << name << endl; while (true) { int number = rand() % 100 + 1; int guess; int tries = 0; char answer; game: while (true) { std::cout << "Enter a number between 0 and 100 " << endl; std::cin >> guess; std::cin.ignore(); if (checkvalidguess() == false) { cout << " enter a number!, not random stuff " << endl; continue; } if (guess < 0 || guess > 100) { cout << "invalid guess, try a guess in range " << endl; tries--; } else if (allreadyguessed(guesses, guess)) { cout << "allready guessed" << endl; } else if (guess > number) { cout << "you guessed way to high " << endl; guesses[index] = guess; index++; } else if (guess < number) { cout << "you guessed to low!try again " << endl; guesses[index] = guess; index++; } else { break; } tries++; } if (guess == guess) { std::cout << "Congratulations!! " << name << std::endl; std::cout << "You guessed the right number in " << tries << " tries "; } while (true) { cout << "would you like to play again ?" "press y for yes and n for no " << endl; cin >> answer; cin.ignore(); if (answer == 'y' || answer == 'Y') { goto game; } else if (answer == 'n' || answer == 'N') { std::cout << "Thank you for playing!"; std::cout << "Enter anything to exit " << std::endl; std::cin.ignore(); break; } else { std::cout << "------------"; } } } return 0; } .``````
Last edited on
You need to reset your guess variable and reload your random number variable before calling goto game, line 132.

To develop it further, you could make the game a 'class' with a 'constructor' which did this.
Hello maksoud,

You have several other problems with your program than just resetting some variables.

Line 44 defines the variable "guess" and line 58 defines the variable "guess". Normally this would be a compiler error, but because of "scope" line 58 is in the scope of the while loop and never sees or used the "guess" defined on line 44. You should do some reading on "scope". http://www.cplusplus.com/doc/tutorial/namespaces/

Avoid using "goto" statements. If the program is done correctly this will not be needed.

Line 120 Did yo mean `if (guess == number)`? As is you are checking "guess" against it-self and it will always be true.

This is from a recent post that is very similar to your program that might help.
http://www.cplusplus.com/forum/beginner/248311/#msg1094799 The rest of that post may also be useful and a response from coder777, in a previous message, has a shorter version.

Line 125 The while loop is not necessary just the contents of the loop. Removing the while loop will also eliminate the need for the "goto".

Hope that helps,

Andy
thanks for the replies, yeah i know that there is things to work on when it comes to the structure, first i am just gonna try to make the program do what i want it to do then i will try to work on that. yeah i will check out the post you linked to thanks
i have managed to reset count and managed to get other numbers randomised but and how to clear my array std::fill(guesses, guesses+100, 0); i think it is correct thanks
Last edited on