need help with my guessing game compiletime errors

Started by
11 comments, last by demonkoryu 19 years, 6 months ago
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;
}
-----------------------------------Panic and anxiety Disorder HQ
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.
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**
now its really screwed up its going
too high
toolow
too high
too low
a million times
-----------------------------------Panic and anxiety Disorder HQ
Hi,

Post your new code, so we can have a look.

Jim.
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;}
-----------------------------------Panic and anxiety Disorder HQ
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.
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;}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

thanks Washu
-----------------------------------Panic and anxiety Disorder HQ

This topic is closed to new replies.

Advertisement