How do I 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. I've had trouble figuring out how to do that (yes i've tried) 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 main()
{
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;
}
Move all code related to actual game to its own function. In main() call this function in a loop until you win.
I tried that. It glitched out my whole game. It'll say its wrong no matter what and it'll only repeat twice.

#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;
}
hi, i don't understand your syntax pretty well, but as long as it yields no error then fine. I am 4 months old c++ student so.. i am no expert :)
here is a code which should works, i am using a different method of generating random numbers so just replace it with yours if you like.

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
#include<iostream>
#include<string>
#include<cstdlib>   
#include<ctime>
using namespace std;

int main()
{
	srand(time(NULL)); //random number generator seed (must be included)
	
	string playerName;
	int num;
	int guess;
	
	cout << "What is your name?" << endl;
	cin >> playerName;
	
	cout << "Hello " << playerName;
	
	do{
		cout<<" ! Guess a number between 1 - 10!"<<endl;
		cin >> guess;
		
		num=1+rand()%10; //generates a random number between 1-10 and sets the value into num
		
		if(guess==num)
		cout << "So you guessed correct! Nice guessing, " << playerName << "!" << endl;
		else //you dont need to use else if and type in another condition, since if they are not equal they are obviously 100% not equal
		cout << "So you guessed wrong! Try again, " << playerName << "!" << endl<<endl;
		
				
	}while(guess!=num); //which means as long as your guess is not equal to the num the loop will start again AND num will have a new random number where if you want the game to continue with the same random number generated, just move the num=1+rand()%10; line outside on top of the loop, and if the player guessed right the loop will stop.

system("PAUSE");
return 0;
}
Last edited on
wow i like it. works perfectly. thanks! I'll be sure to keep a copy of this to look at in times of trouble :P
Topic archived. No new replies allowed.