Can someone help please i have written code for random number gen game but wont compile

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main(void)
{
    int Oliver = rand();
    int User_guess;
    
    cout << "Guess my Number!" << endl;
    loop:
    cin >> User_guess;
    
    if (User_guess = Oliver)
       cout << "Well Done! You got it right!" << endl;
       
    else if (User_guess < Oliver)
         {
                        cout << " Too Low! Guess again:" << endl;
                        goto loop;
         }
    
    else 
         {
                        cout << "Too High! Guess again:" << endl;
                        goto loop;
         }
    
    
    system("PAUSE");
    return 0;
}


If someone could look it over and tell me where i need to change code lines i would be very grateful.
It compiled fine for me....
EDIT:
But I tried it and no matter what number I enter I win
Last edited on

* Where is the rand() function defined... ? You should include cstdlib...

* You should seed the random number generator using srand http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

*The "always winning" thing is down to one of the classic beginner bugs... ;-)
I commented it for you, the srand() function will seed to your rand() function algorithm and make sure its always a random number generated. Make sure to only call srand() no more than once in your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ctime>
using namespace std;

int main(void)
{
	srand(time(0));
	int MAX_RANGE = 100; // The max range from 1-100 of rand();
	int Oliver = rand() % MAX_RANGE + 1; //Initializes Oliver to = rand() with the set MAX_RANGE starting from 1
	int User_guess;

	//Basically will run the program over and over until you guess correctly.
    do{

	cout << "What is my number: ";
	cin >> User_guess;
	
	if(User_guess == Oliver)
		cout << "You guessed correctly!\n";
	else if(User_guess < Oliver)
		cout << "You guessed to low! Try again!\n";
	else if(User_guess > Oliver)
		cout << "You guessed to high! Try again!\n";
	}while(User_guess != Oliver);
	


    cin.get(); //Dont use system, just use cin.get() if you want a pause.
    cin.get();
    return 0;
}
Last edited on
eaxx, you forgot to include cstdlib....
What are you talking about? This compiles fine
It might compile fine for you, but it may be that your compiler is automagically including cstdlib for you.

srand() and rand() are defined in cstdlib so that header should be included. Simples.
Get a better compiler then.
Get a better compiler then.

Please don't make such statements if you don't know what you're talking about.
emyr666 is right.

When you are going to use functions from a header, you have to include it (obviously).
It doesn't count if another header (like <iostream>) happens to include <cstdlib> by chance. That's an implementation detail and it is nothing you are allowed to rely on. It might just as well change in the next update to your standard library implementation.
Last edited on
Thanks Athar

I will include the <cstdlib> library into the code and see if it works :)

Thanks
It still wont compile even with the <cstdlib> included?

What other compiler would you recommend because i have reason to believe it is something to do with my compiler?
Hint: read the compile errors
Topic archived. No new replies allowed.