Code for Replaying a Game w/o Restarting it?

• 04-21-2007
Code for Replaying a Game w/o Restarting it?
Hi I made this game just for the fun of it:
Code:

```#include <stdafx.h> #include <iostream> #include <cstdlib> #include <time.h> using namespace std; int main() {         const int lowest = 1;         const int highest = 999;         int nNumber, nGuess, nNoOfGuesses;         time_t seconds;         time(&seconds);         srand((unsigned int) seconds);         nNumber = rand() % (highest - lowest + 1) + lowest;                 cout<<"Guess the Number - by UPNPAD" <<endl <<"Hey you! Lets play guess the number! Okay, the rules are: " <<endl;         cout<<"1. The number is anything from 1-999" <<endl <<"2. You have 10 guesses to get the number" <<endl;         cout<<"3. If you get it wrong, I'll tell you if you should guess higher or lower next  time." <<endl <<"OK! Let's Play!" <<endl;         for (nNoOfGuesses = 1; nNoOfGuesses < 11; nNoOfGuesses++){                 cout<< "Please type your guess: " <<endl;                 cin>>nGuess;                 cin.ignore();                 if (nGuess < nNumber){                         cout<<"Try guessing higher." <<endl;                 }                 else if (nGuess > nNumber){                         cout<<"Try guessing lower." <<endl;                 }                 else if (nGuess = nNumber){                         cout<<"Well done! You guessed correctly!" <<endl;                         cin.get();                         return 0;                 }         }         cout<<"You didn't manage to guess the number in 10 tries. The answer you needed was " <<nNumber <<"  :( Better luck next time!" <<endl;         cin.get();         return 0; }```
I'm wondering how, after I either win or lose at the game, I would give an option to restart the game. Possibly a switch case? And how would I define the replay point in the game?
• 04-21-2007
Dave_Sinkula
Sure, put the game itself in a loop. There are several thousand threads about this, generally involving doing so incorrectly looking for yes/no replies.
• 04-22-2007
Salem
Play 1 game
Code:

```int main ( ) {   playGame();   return 0; }```
Play several games
Code:

```int main ( ) {   do {     playGame();     cout << "Play again? ";     cin >> answer;   } while ( answer == 'y' );   return 0; }```