Code for Replaying a Game w/o Restarting it?

This is a discussion on Code for Replaying a Game w/o Restarting it? within the C++ Programming forums, part of the General Programming Boards category; Hi I made this game just for the fun of it: Code: #include <stdafx.h> #include <iostream> #include <cstdlib> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Question 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?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 09:37 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Example of code for tic tac toe game?
    By carlin70 in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2003, 11:18 AM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21