number guessing game

This is a discussion on number guessing game within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <time.h> using namespace std; int rnd (void) { return (rand() % 100 + 1); // returns ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    number guessing game

    Code:
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    
    int rnd (void) 
    {
    	return (rand() % 100 + 1);	// returns a number between 1 and 100
    }
    
    
    void main (void)
    {
    	srand (time(NULL));		// to seed the random number generator
    	
    	int guess;
    	int choice;
    	char input;
    	bool playagain;
    	int number;
    	playagain = false;
    	while (!playagain)
    	{
    		cout << "Welcome to my number guessing game! " << endl;
    		cout << "Which game do you want to play ?" << endl;
    		cout << " User to guess a number generated randomly by the program  " << endl;
    		cout << "Select number '1' for this game" << endl;
    		cout << " User think up a number for the program to guess  " << endl;
    		cout << "Select number 2 for this game" << endl;
    	
    		cout << "Select your choice: " ;
    		cin >> choice;
    
    		while (choice < 1 || choice > 2)
    		{
    			cout << "Please enter '1' or '2' of the number only" << endl;
    			cout << "Enter again: " ;
    			cin >> choice;
    		}
    
    		
    		if (choice == 1)
    		{
    			bool isGuessed;
    			int num = rnd();
    			isGuessed = false;
    
    			cout << "You had selected the first game" << endl;
    			cout << "Welcome to my first part of the game" << endl;
    			while (!isGuessed)
    			{
    
    				cout << "Enter an integer greater or equal to 0 and less than 100: ";
    				cin >> guess;
    				while (guess < 1 || guess > 100)
    				{
    					cout << "Your number had been out of range, please enter number between 1 to 100 only." << endl;
    					cout << "Enter again: " ;
    					cin >> guess;
    				}
    				cout << endl;
    	
    
    				if (guess == num)
    				{
    					cout << "You guessed the correct number" << endl;
    					cout << "My random number is " << num << endl;
    					cout << "Thank you for playing my number guessing game." << endl;
    
    					isGuessed = true;
    				}
    				else if (guess < num)
    				{
    		
    					cout << "Your number is smaller.Guess again";
    					cout << endl;
    				}
    		
    				else 
    				{
    					cout << "Your number is higher. Guess again";
    					cout << endl;
    				}
    			}
    		}
    		else if (choice == 2)
    		{
    			
    			bool iscorrect = false;
    			
    			cout << "You had choose for the program to guess the number for you to" << endl;
    			cout << "input it" << endl;
    			cout << "Welcome to my second part of my game. " << endl;
    			
    			while (!iscorrect)
    			{
    				int num = rnd();
    				cout << num << endl;
    				
    				
    				
    				cout << "Is this your number, L stand for too low, H stand for too high and " << endl;
    				cout << "C stand for correct" << endl;
    				cin >> input;
    				cout << endl;
    
    				if (input  == 'C' || input == 'c')
    				{
    					cout << "The program has guess your number correctly" << endl;
    					cout << "This is your number I had guess correctly : " << num << endl;
    					cout << "Thank you for playing my number guessing game." << endl;
    					iscorrect = true;
    				}
    				else if (input == 'H' || input == 'h')
    				{
    					cout << "the program guess too high" << endl;
    
    				}
    				else if (input == 'L' || input == 'l')
    				{
    					cout << "the program guess too loww" << endl;
    				}
    				else
    				{
    					cout << "Invalid input" << endl;
    				}
    			}
    		}
    		cout << "Do you want to play again. " << endl;
    		cout << "Press 1 to play agin or press 2 to stop the game." << endl;
    		cin >> number;
    
    		while (number < 1 || number > 2)
    		{
    			cout << "Press only 1 or 2. " << endl;
    			cout << "Press a number again." << endl;
    			cin >> number;
    		}
    
    		if (number == 1)
    		{
    			cout << "Lets play again. " << endl;
    			
    		}
    		else
    		{
    			cout << "Thank you for playing" << endl;
    			playagain = true;
    		}
    	}
    		
    		
    	
    	
    			
    			
    		
    	
    	system ("pause");
    }
    Hello, i am back again. I need help. I got a problem. For my part 2 game where the program would print a random number and the user are to tell the computer that if the number is too high or too low or the random number is correct. But, if the user said number is too low, but still the random number would still give a lower number . Is there any techniques to code such that if the user said the number is low, the computer would random a higher number.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Cprogramming.com FAQ > Generate random numbers?
    For getting a random number between two limits.

    The idea being, if the user inputs 'H', then the number you guessed is your new upper bound.

    Also, consider this as a way of improving your code structure (so it's not all in main).
    Code:
    if (choice == 1) {
      playHumanGuesses();
    } else
    if (choice == 2) {
      playComputerGuesses();
    }
    Move appropriate blocks of code into new functions.
    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.

  3. #3
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    Class Based

    Per Salem's tip here is some class based code using your original code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iostream>
    
    using namespace std;
    
    
    int randNum()
    {
        return ((rand() % 100) + 1);
    }
    
    class numGame
    {
    
    public:
        numGame() {};
        void progGen();
        void userGen();
    
    };
    
    void numGame::userGen()
    {
        bool iscorrect = false;
        char input;
    
        cout << "You had choose for the program to guess the number for you to" << endl;
        cout << "input it" << endl;
        cout << "Welcome to my second part of my game. " << endl;
        while (!iscorrect)
        {
            int num = randNum();
            cout << num << endl;
    
            cout << "Is this your number, L stand for too low, H stand for too high and " << endl;
            cout << "C stand for correct" << endl;
            cin >> input;
            cout << endl;
    
            if (input  == 'C' || input == 'c')
            {
                cout << "The program has guess your number correctly" << endl;
                cout << "This is your number I had guess correctly : " << num << endl;
                cout << "Thank you for playing my number guessing game." << endl;
                iscorrect = true;
            }
            else if (input == 'H' || input == 'h')
            {
                cout << "the program guess too high" << endl;
    
            }
            else if (input == 'L' || input == 'l')
            {
                cout << "the program guess too loww" << endl;
            }
            else
            {
                cout << "Invalid input" << endl;
            }
        }
    }
    
    
    void numGame::progGen()
    {
    
        bool isGuessed=true;
        int num = randNum();
        int guess;
    
        cout << "You have selected the first game." << endl;
        cout << "Welcome to my first part of the game" << endl;
        while (isGuessed)
        {
    
            cout << "Enter an integer greater or equal to 0 and less than 100: ";
            cin >> guess;
            while (guess < 1 || guess > 100)
            {
                cout << "Your number had been out of range, please enter number between 1 to 100 only." << endl;
                cout << "Enter again: " ;
                cin >> guess;
            }
            cout << endl;
    
    
            if (guess == num)
            {
                cout << "You guessed the correct number" << endl;
                cout << "My random number is " << num << endl;
                cout << "Thank you for playing my number guessing game." << endl;
    
                isGuessed = false;
            }
            else if (guess < num)
            {
    
                cout << "Your number is smaller.Guess again";
                cout << endl;
            }
    
            else
            {
                cout << "Your number is higher. Guess again";
                cout << endl;
            }
        }
    }
    
    
    int main ()
    {
        srand(time(NULL));
        numGame ng;
        int choice;
        bool playagain=true;
    
        char playG;
        try
        {
            while (playagain)
            {
                cout << "Welcome to my number guessing game! " << endl;
                cout << "Which game do you want to play ?" << endl;
                cout << " User to guess a number generated randomly by the program  " << endl;
                cout << "Select number '1' for this game" << endl;
                cout << " User think up a number for the program to guess  " << endl;
                cout << "Select number 2 for this game" << endl;
    
                cout << "Select your choice: " ;
                cin >> choice;
    
                while (choice < 1 || choice > 2)
                {
                    cout << "Please enter '1' or '2' of the number only" << endl;
                    cout << "Enter again: " ;
                    cin >> choice;
                }
    
                if (choice == 1)
                {
                    ng.progGen();
                }
                else if (choice == 2)
                {
                    ng.userGen();
                }
    
                bool guessPlay=true;
                while (guessPlay == true)
                {
                    cout << endl << "Do you want to play again? ";
                    cin >> playG;
                    if ((playG == 'Y') || (playG == 'y'))
                    {
                        playagain = true;
                        guessPlay = false;
                    }
                    else if ((playG == 'N') || (playG == 'n'))
                    {
                        playagain = false;
                        guessPlay=false;
                    }
                    else
                    {
                        cout << endl << "Enter Y or N" << endl;
                    }
                }
            }
        }
        catch (exception& e)
        {
            cout << "Exception thrown " << e.what() << endl;
            return 1;
        }
       return 0;
    }
    I tested it a few times, but might still be some things I missed.
    Last edited by xentaka; 05-22-2011 at 07:01 PM. Reason: Forgot to add return on int main()

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Why is it that no one complained about void main?
    SourceForge.net: Void main - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    Well

    I changed it to int on rewrite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with number guessing game
    By iceangel393 in forum C Programming
    Replies: 1
    Last Post: 02-23-2011, 05:52 PM
  2. Number Guessing Game Help?
    By zacharyrs in forum C Programming
    Replies: 10
    Last Post: 10-01-2009, 07:49 PM
  3. Help!!! Number guessing game
    By JesterJoker89 in forum C++ Programming
    Replies: 16
    Last Post: 10-05-2007, 12:47 PM
  4. My number guessing game
    By The Gweech in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2002, 09:03 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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