need help

ikusung's Avatar, Join Date: Mar 2010
Newbie Member
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?

Last edited by shabbir; 12Mar2010 at 15:58.. Reason: Code blocks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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();
}