Sign in to follow this  
hothead

need help with my guessing game compiletime errors

Recommended Posts

hothead    767
I've got a bad error when you enter a number it will keep printing too low or too high forever heres the code
#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;
}

Share this post


Link to post
Share on other sites
JimPrice    815
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.

Share this post


Link to post
Share on other sites
Cosmic One    308
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**

Share this post


Link to post
Share on other sites
hothead    767
here
#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;
}


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)

cout<<"\nthats too low";
if(MyGuess==Num)
cout<<"\n you gessed it in"<<tries<<"tries";
}
cin>>MyGuess;
return 0;
}

Share this post


Link to post
Share on other sites
JimPrice    815
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.

Share this post


Link to post
Share on other sites
JimPrice    815
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;
}




Share this post


Link to post
Share on other sites
Washu    7829

#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;
}

Share this post


Link to post
Share on other sites
JimPrice    815
Washu,

Just out of interest - is there any reason for changing to a do-while loop instead of a while loop other than:
a) you want to go through the loop at least once
b) personal preference

Thanks,
Jim.

Share this post


Link to post
Share on other sites
Carnage_frank    122
Basically the only difference between a do..while and a while loop is like you said, a do...while loop executes at least once and a while loop has its exit expression evaluated before entering the loop.

Both loop style's consume about the same amount of memory to execute.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this