Jump to content
  • Advertisement
Sign in to follow this  
hothead

need help with my guessing game compiletime errors

This topic is 5042 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!