How do I successfully loop back to the beginning

So I'm making my first program w/ C++ and its a game guessing game. I've learned how to use booleans, chars, strings, if/else, loops, and input. Anything past that I dont know. I've gotten my game to work properly except for the fact that when you guess incorrectly, the program closes. I want the program to go to the beginning of the program again so the user can restart. So far i've gotten the loop to beginning thing to work but not well. It'll only repeat twice before closing again and it'll say the user got the wrong answer even if it was correct. I just need help with this and this only. I'm just started and i just started learning 3 days ago. Any help would be greatly appreciated and some helpful tips on advancing or organizing my code better would also be helpful but isnt needed. Here's my code

#include <iostream>
#include <string>
#include <random>
#include<ctime>

using namespace std;

int game(){
string playerName;
int guess;

cout << "What is your name?" << endl;

cin >> playerName;




cout << "Hello " << playerName << " ! Guess a number between 1 - 10!" << endl;
cin >> guess;


// random number gen
default_random_engine randomGen(time(NULL));
uniform_int_distribution<int> number(1, 10);
//loop
if ((guess >= 1) || (guess <= 10)) {
cout << "The number you guesed was... " << guess << " The number that was generated was... " << number(randomGen) << endl;
int num = number(randomGen);
//checking if guess was correct
if (guess == num){
cout << "So you guessed correct! Nice guessing, " << playerName << "!" << endl;
}
else if (guess != num) {
cout << "So you guessed wrong! Try again, " << playerName << "!" << endl;
}
}
system("PAUSE");
return 0;
}
int main(){
int Game;
//Game = game();
do {
Game = game();
} while (Game = game());
system("PAUSE");
return 0;
}
You should put your program in condition loop
Example below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
int gameon=1;
while gameon=1
{
	// Play game
	
	cout << "would you like to play again ?  Y/N ";
	cin << answer;
	if (answer=N)
		{
		gameon=0; // end game
		}
}
Last edited on
Topic archived. No new replies allowed.