Sign in to follow this  

need help with my guessing game compiletime errors

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
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
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

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

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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