Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   need help (http://www.go4expert.com/forums/need-help-t21325/)

ikusung 12Mar2010 14:42

need help
 
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";
}
}
}

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?

virxen 13Mar2010 04:01

Re: need help
 
check the changes and ask what you do not understand.

Code:

#include <iostream>
#include <time.h>
using namespace std;

int main() {
int brian=-1;
int happy;
int r=0;

srand(time(NULL));
r = rand () %101; //let it outside the loop in order to get 1 value only for each run of the program
cout <<r << "\n";

while (brian!=r) {

cout << "enter number:" ;
cin >> brian;
if (brian == r) {
    cout << "Go you, you win\n";
}
else if (r<brian){
    cout << "number x is Lower\n";
}
else {
    cout << "number x is Higher\n";
}

}
getchar();getchar();
}



All times are GMT +5.5. The time now is 05:37.