Thread: Random Number Guessing Game

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    7

    Random Number Guessing Game

    Im a beginner, and I have a problem dealing with random number game.
    Im post to allow the user to have 10 tries before the game exits or restarts with another random number, but im having trouble figuring out how to do that.
    Also, ive been trying to make a fuction for the code but its not coming out the way i planned.
    Any help or tips will do, thanks. =]


    Code:
    /* 
     * File:   main.cpp
     * Author: Matthew
     *
     * Created on July 25, 2012, 12:55 AM
     */
    #include <iostream>
    #include <ctime>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    int randomNum(int guess)
    {
        srand(time(0));
        int rnum= rand()%5;
        char again = 'y';
        do
        {
            if (rnum < guess)
            {
                cout<<"Too low, Try again. ";
                cin>>guess;
            }
            else if (rnum > guess)
            {
                cout<<"Too high, Try again. ";
                cin>>guess;
            }
            else if ( rnum == guess)
            {
                cout<<"You have found the random number!";
            }
            else
            {
                cout<<"Would you like to play again? (y or n)"<<endl;
                cin>>again;
            }
        }while (again = 'Y' || again = "y");
        
    }
    
    int main(int argc, char** argv) {
        
        srand(time(0));
        int guess;
        cout<<"I have a number between 1 and 1000."<<endl;
        cout<<"Can you guess the number?"<<endl;
        cout<<"You will have a maximum of 10 guesses."<<endl;
        cout<<"Please try your first guess and good luck!"<<endl<<endl;
        cin>>guess;
        randomNum(guess);
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Im post to allow the user to have 10 tries before the game exits or restarts with another random number...
    Something like this, perhaps?

    Code:
    int maxGuess = 0;
    
    do
    {
        // code
        ++maxGuess;
    } while(maxGuess < 10);
    Also, in this line:

    Code:
    while (again = 'Y' || again = "y");
    - You should be using the relational operator (==) instead of the assignment operator (=)
    - You want lowercase y to be in single quotes, not double quotes

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    Quote Originally Posted by Matticus View Post
    Something like this, perhaps?

    Code:
    int maxGuess = 0;
    
    do
    {
        // code
        ++maxGuess;
    } while(maxGuess < 10);
    i tried this and it worked.. thanks
    So this is what i now have

    Code:
    #include <iostream>
    #include <ctime>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    int randomNum(int guess, int maxGuess)
    {
        srand(time(0));
        int rnum= rand()%5;
        char again = 'y';
       
        do
        {
            if (rnum > guess)
            {
                cout<<"Too low, Try again. ";
                cin>>guess;
            }
            else if (rnum < guess)
            {
                cout<<"Too high, Try again. ";
                cin>>guess;
            }
            else if ( rnum == guess)
            {
                cout<<"You have found the random number!";
            }
            else
            {
                cout<<"Would you like to play again? (y or n)"<<endl;
                cin>>again;
            }++maxGuess;
        } 
        while (maxGuess < 10);
        
    }
    
    int main(int argc, char** argv) {
        
        
        int guess;
     int maxGuess=0;
        cout<<"I have a number between 1 and 1000."<<endl;
        cout<<"Can you guess the number?"<<endl;
        cout<<"You will have a maximum of 10 guesses."<<endl;
        cout<<"Please try your first guess and good luck!"<<endl<<endl;
        cin>>guess;
        randomNum(guess,maxGuess);
        
        return 0;
    }



    Quote Originally Posted by Matticus View Post
    Also, in this line:

    Code:
    while (again = 'Y' || again = "y");
    - You should be using the relational operator (==) instead of the assignment operator (=)
    - You want lowercase y to be in single quotes, not double quotes
    but i had to put it in place of the while loop that would restart the game... and i still want to restart the game.. any suggestions??

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    Also, make sure it's
    Code:
    int rnum= (rand()%1000) + 1;
    since you want the random number between 1 to 1000.

    basic mod operator is: any integer % divisor will give you a range b/t 0 to (divisor - 1).

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    yeah i got that.. i just had it low to test the if statements easyer...

    do you have any suggestions on how I could have the user select whether or no they would like to play again??
    i know i would have to use a do while statement just don't know where to put it....

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    but i had to put it in place of the while loop that would restart the game... and i still want to restart the game.. any suggestions??
    Use nested "while" loops. The outer loop will deal with restarting the game, and the first thing it would do is pick a random number. The inner loop will deal with ten guesses. When the inner loop ends (10 guesses were made or the correct number was guessed), and the user wishes to continue, execution will jump back to the first line of the outer loop (generating a new random number) and start the inner loop (guessing part) over again.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    Quote Originally Posted by Matticus View Post
    Use nested "while" loops. The outer loop will deal with restarting the game, and the first thing it would do is pick a random number. The inner loop will deal with ten guesses. When the inner loop ends (10 guesses were made or the correct number was guessed), and the user wishes to continue, execution will jump back to the first line of the outer loop (generating a new random number) and start the inner loop (guessing part) over again.
    would i create that with two do while loops?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sure, that would work. Have you ever practiced with nested loops?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    iMattbckr, are you familiar with flow charts? If you aren't, then read up about them and do your logic in a flowchart. I strongly recommend you do the logic for your project in a flowchart, then translate that to code.
    Matticus, don't hand out solutions, especially problems regarding logic. This is a basic skill every programmer must master, and the best way to do is to practice.
    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.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    Just a question can you do this game with fuctions?
    because i wasnt using functions i would be finished already, but since im taking a summer class and we are currently learning them (at a rapid rate) i was assuming i would have to use a function of some sort...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMattbckr
    no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?
    You can search the Web.

    Quote Originally Posted by iMattbckr
    Just a question can you do this game with fuctions?
    Yes.

    Quote Originally Posted by iMattbckr
    because i wasnt using functions i would be finished already, but since im taking a summer class and we are currently learning them (at a rapid rate) i was assuming i would have to use a function of some sort...
    The part that you appear to be having trouble with is the logic rather than how to write/use a function, hence the suggestion of a flow chart. As such, I doubt that you would be finished already either way
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2012
    Posts
    7
    so would this be logic...

    1: start
    2: get random number for user to guess (rnum)
    3: ask user to input a number (guess)
    4: determine if numbers are same (guess = rnum)
    5: if numbers are same congradulate!
    6: ask if they would like to play agian ( y or no)
    7: yes -> go back to step 2
    8: no -> stop

    9: if guess is less than rnum
    10: ask user for higher guess

    11: if guess is greater than rnum
    12: ask user for lower guess

    13: do either step 9 or 11 untill rnum and guess equal

    14: however if guess exceeds 10 inputs
    15: notify user of tries and stopping inputs
    16: ask user if they would like to try agian (Y or N)


    17: Y -> go back to step 2
    18: N -> stop program

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You aren't gaining anything by passing maxGuess in to randomNum, you may as well just make that a local variable to the randomNum function. Of course you could pass in the number 10 and then decrement that until zero instead (or count up to that using a separate variable), which would actually be somewhat useful.

    You should more this line to where you had it in main and delete it from where it is now:
    Code:
    srand(time(0));
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMattbckr View Post
    no im not familiar with flow charts... would i beable to find a link on this site or could you direct me to one?
    Please, search the web for simple stuff like this. At least give it a try before asking about it. You are expected to do at least that much.
    If you did so, then clearly state so in your reply to avoid being told to search the web. We are not here to babysit you.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a random number guessing game (exit failure? wtf)
    By muffinman8641 in forum C++ Programming
    Replies: 36
    Last Post: 03-04-2011, 10:09 AM
  2. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM