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
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;
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.
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
}