#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(0));//seed the random generater
int Num = rand() % 100 + 1;
int tries = 0, MyGuess;
cout<<"enter your guess";
cin>>MyGuess;
while((Num)!=(MyGuess))
{
++tries;
if(MyGuess>Num)
cout<<"\nthats too high";
if(MyGuess<Num)
cout<<"\nthats too low";
if(MyGuess==Num)
cout<<"\n you gessed it in"<<tries<<"tries";
}
return 0;
}
need help with my guessing game compiletime errors
I've got a bad error when you enter a number it will keep printing too low or too high forever heres the code
Hi,
You only enter your response outside the while loop (that is, MyGuess never gets updated). Therefore, at the end of the loop, it goes back to the beginning and the same clause is still true (too high / too low) and therefore it keeps looping.
Move the cout / cin to be the first statements inside the loop.
Jim.
You only enter your response outside the while loop (that is, MyGuess never gets updated). Therefore, at the end of the loop, it goes back to the beginning and the same clause is still true (too high / too low) and therefore it keeps looping.
Move the cout / cin to be the first statements inside the loop.
Jim.
I'd put another cin statement inside your while loop cuz otherwise that thing keeps checking the same condition over and over again. If the thing says while ((Num)!=(MyGuess)) and you never change myguess then the thing will repeat endlessly. Depending on where you add this you might need an exit statement to make it leave before you ask it again.
Hope this helps,
As ever,
**Cosmic**
Hope this helps,
As ever,
**Cosmic**
here and I've tried it like this to
#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){ srand(time(0));//seed the random generater int Num = rand() % 100 + 1; int tries = 0, MyGuess; cout<<"enter your guess"; while((Num)!=(MyGuess)) { ++tries; if(MyGuess>Num) cout<<"\nthats too high"; if(MyGuess<Num) cin>>MyGuess; cout<<"\nthats too low"; if(MyGuess==Num) cout<<"\n you gessed it in"<<tries<<"tries"; } return 0;}
#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){ srand(time(0));//seed the random generater int Num = rand() % 100 + 1; int tries = 0, MyGuess; cout<<"enter your guess"; while((Num)!=(MyGuess)) { ++tries; if(MyGuess>Num) cout<<"\nthats too high"; if(MyGuess<Num) cout<<"\nthats too low"; if(MyGuess==Num) cout<<"\n you gessed it in"<<tries<<"tries"; }cin>>MyGuess; return 0;}
Hi,
The second example didn't seem to take, but...
Take your original code.
Put the cout<<"enter your guess" and cin>>MyGuess lines as the first lines after you start the loop (so immediately after the opening loop curly bracket and before ++tries.
See if that works.
Jim.
The second example didn't seem to take, but...
Take your original code.
Put the cout<<"enter your guess" and cin>>MyGuess lines as the first lines after you start the loop (so immediately after the opening loop curly bracket and before ++tries.
See if that works.
Jim.
Like this:
#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){ srand(time(0));//seed the random generater int Num = rand() % 100 + 1; int tries = 0, MyGuess; while((Num)!=(MyGuess)) { cout<<"enter your guess"; cin>>MyGuess; ++tries; if(MyGuess>Num) cout<<"\nthats too high"; if(MyGuess<Num) cout<<"\nthats too low"; if(MyGuess==Num) cout<<"\n you gessed it in"<<tries<<"tries"; } return 0;}
#include<iostream>#include<ctime>#include<cstdlib>using namespace std;int main(){ srand(time(0));//seed the random generater int Num = rand() % 100 + 1; int tries = 0, MyGuess; cout<<"enter your guess: "; do { cin>>MyGuess; ++tries; if(MyGuess>Num) cout<<"\nthats too high "; if(MyGuess<Num) cout<<"\nthats too low "; if(MyGuess==Num) cout<<"\n you gessed it in "<<tries<<"tries"; } while(MyGuess != Num); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement