#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?
|
Mentor
|
![]() |
| 13Mar2010,13:09 | #2 |
|
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";
}
}
}
Code:
r = rand () %101; |
|
Mentor
|
![]() |
| 13Mar2010,13:11 | #3 |
|
By the way, rand() returns a new random number each time it is called, so
Code:
cout <<rand () %101 << "\n"; |

