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

1. ## 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. How would you do it in pseudo code, or a flowchart?

3. 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.

4. 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. 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?

6. 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.

7. 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.

8. OK I can try to learn me how to use a debugger.

9. 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. 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.

11. 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..?

12. 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. 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

14. Yes! got the program to work now. Thanks all!.