Thread: Problem with coding "Random Number Guessing Game" (Beginner)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Problem with coding "Random Number Guessing Game" (Beginner)

    Problem with coding "Random Number Guessing Game"

    I am beginner in C++ and have written a little bit code to the "Random Number Guessing Game" but I don't know how to code the last bit of code the code looks like this

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int randRange(int high, int low)
    {
        return rand() % (high - low) + 50;
    }
    void getRand()
    {
        srand(time(NULL));
        for (int i = 0; i < 1; ++i)
        {
            cout << randRange(50, 0) << '\n';
        }
    }
    int main()
    {
        int number;
        getRand();
        cout << "Guess the number: ";
        cin >> number;
        if (number ==)
    }
    After "if (number ==)" I don't know how to continue tried a few things but no one of them worked.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How would you do it in pseudo code, or a flowchart?
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    getRand doesn't actually "get" anything. It just prints a number on the screen. If you want to know what the number was then you'll need to store it somewhere, in a variable.

    You've also completely missed the point of how a randRange function is meant to be used. It shouldn't have hard-coded numbers like 50 in it. That's what the input arguments are for.
    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"

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    with getRand I mean get random number I know that it only prints a random number to the screen but I want a user to guess what the number is and the program will say if the number is too low, too high or correct.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The program cant tell the user if their guess is too high or too low if it doesn't know what the random number was.
    You need to remember it in a variable.

    Besides, printing the value on screen makes it a bit easy for the user to guess don't you think?
    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"

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Yes it is a bit easy with having the random number before the users guesses but I had to test if my program worked when you typed in the right number and it didn't worked. But I will change that later. But sorry if you thinks I am dumb but how do you store/remember getRand() in a variable tried "int number = getRand();", "int number == getRand;". getRand let's me print a random number to the screen but how I store getRand() in a variable I don't know how to do that.
    Last edited by DecoratorFawn82; 02-20-2013 at 04:00 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your randRange function generates a random number. What getRand seems to do is just print the random number to screen.
    As for printing the random number to screen, it would be far better and far easier to learn how to use a debugger. In case you're lazy, Visual Studio provides one built-in and there are many tutorials on how to use the IDE.
    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.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    OK I can try to learn me how to use a debugger.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    13
    I'm not really sure why you have a loop in your getRand() function. Is that so you can generate more numbers later on? It seems like it's just there to be there, and adding code that is not needed is one of the easiest ways to mess things up.

    I would go look up the rand() function first in the reference pages and be absolutely sure what you want to accomplish using that function is actually what is being done.

    Also, you may want to re-read what the return data types do when you define a function and what the differences are between say a void function, and an int function.

    Using that knowledge of different function return types you can then use the advice that someone gave you earlier (a hint: someone in this thread already stated that you need to store something in a variable).

    Once you have those pieces of knowlege you should be on your way to solving this problem, based on what you have in your code already to "check" the number, the rest should become clear to you.

  10. #10
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    I'm wonder why I had a loop in my program too it isn't needed at all. That I want the program to do is to print a number between 1 - 100 and then the program will let the user guess the number I have changed the code now so it looks like this so far.

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int randRange(int high, int low) 
    {
        return rand() % (high - low) + 10;
    }
    int getRand() 
        {
        srand(time(NULL));
        {
            cout << randRange(90, 10) << '\n';
        }
        }
    int main()
    {
        int number;
        getRand();
        cout << "Guess the number: ";
        cin >> number;
        if (number == "?")
        {
            cout << "YES";
        }
        else
        {
            cout << "NO";
        }
    }
    This have I changed the code to but I'm still thinking on " how to store something in a variable " The program says if a number is wrong now but I'm know that it's still not working beacause it says even if I enter the correct number so says the program that it's wrpng number.
    Last edited by DecoratorFawn82; 02-26-2013 at 02:09 PM.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You store values in a variable by declaring one - But you already know that because your function takes two arguments high and low and you declare , 'number' in main??
    The program always says it is the wrong number because in randRange() all you do is output the result of rand() - The rest of your program knows nothing about this result...so you can make your function return it perhaps..?
    Last edited by rogster001; 02-26-2013 at 02:27 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question

    Hmm. . . thinking on how to do that. But haven't get it yet. That I know is that I must "Store something in a variable" and that my getRand() function only prints a random number between 1 - 100 but I want the program to get the random number so it can knew what the number is and if the user enters wrong/right number the program will say "Right number"/"Wrong Number". But I'm not really sure of how to do this I tried to store getRand() in a variable but of course so prints it just the random number again after the first random number. Thanks for giving me hints but I haven't got it yet.

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you just declare your function to return a number instead of void and then in main you assign that result into a variable from the function call, then it is tested in your if statement
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Yes! got the program to work now. Thanks all!.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Guessing Game
    By Borko Kovacev in forum C Programming
    Replies: 2
    Last Post: 02-13-2013, 03:14 AM
  2. Random Number Guessing Game
    By iMattbckr in forum C++ Programming
    Replies: 14
    Last Post: 07-26-2012, 04:00 AM
  3. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 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