Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need Help (http://www.go4expert.com/forums/need-help-t21326/)

ikusung 12Mar2010 14:50

Need Help
 
#include <iostream>
#include <time.h>
using namespace std;
void main() {
srand(time(0));
while (true) {
int brian;
int happy;
int r;
r = rand () %101;
cout <<rand () %101 << "\n";
cout << "enter number \n" ;
cin >> brian;
if (brian == r) {
cout << "Go you, you win\n";
}
else if (r<brian){
cout << "Lower\n";
}
else {
cout << "Higher\n";
}
}
}

Could anyone point out what's wrong with my code .
It's meant to be a higher or lower game but it keeps generating a new number everytime?

xpi0t0s 13Mar2010 13:09

Re: Need Help
 
If you format the code correctly the problem should be obvious:
Code:

#include <iostream>
#include <time.h>
using namespace std;
void main() {
        srand(time(0));
        while (true) {
                int brian;
                int happy;
                int r;
                r = rand () %101;
                cout <<rand () %101 << "\n";
                cout << "enter number \n" ;
                cin >> brian;
                if (brian == r) {
                        cout << "Go you, you win\n";
                }
                else if (r<brian){
                        cout << "Lower\n";
                }
                else {
                        cout << "Higher\n";
                }
        }
}

If you don't want a new number on every iteration, why is
Code:

r = rand () %101;
inside the while loop?

xpi0t0s 13Mar2010 13:11

Re: Need Help
 
By the way, rand() returns a new random number each time it is called, so
Code:

cout <<rand () %101 << "\n";
would not be expected to display r. If you want it to display r, you have to tell it to display r.


All times are GMT +5.5. The time now is 03:18.