problems looping for my first time.

closed account (1R91hbRD)
I'm creating a program that generates a random number from 1-100, then it asks the person to guess the number. If the number is too low it will tell them its too low and try again. if its too high the program will tell them its too high and tells them to try again, until they guess they right number.

The problem is that my program randomly ends without the correct answer being finalized. Sometimes it does finish, but sometimes it doesn't. I don't know why its doing that, please help!!! Thanks ahead of time!
Here's my code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
system("color C0"); //gives a appealing color to the background and symbols.

int num1,guess1;

int srand(time(NULL)); //seeds the generator

//generates a random number
num1 = rand() % 100 +1;

cout << "I'm thinking of a number from 1-100." <<endl;
cout << "Can you guess what it is? "<<endl;
cin >> guess1;



while(guess1 < num1)
{
cout << "Too low.. try again." << endl;
cin >> guess1 ;
}

while(guess1 > num1)
{
cout <<"Too high.. try again." <<endl;
cin >> guess1;

}
if(guess1 == num1)
{

cout << "Congratulations!!! You guessed the right number!";
cout << "The number was indeed " << num1 <<"."<<endl;

}

return 0;
}
The problem is, if the initial guess is too high, the second while loop is entered.

Then if a later guess is too low, there's no way to output the "too low" message, as the program has already passed that point.

What you need to do is change those two "while"s into if-statements.
And then wrap both the new if statements in a while loop which has (guess1 != num1) as its controlling condition.

Last edited on
closed account (1R91hbRD)
Thank you so much! i sort of understand it! This is what i have now.
(I'm still figuring out how to stop infinite loops)



#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
system("color C0"); //gives a appealing color to the background and symbols.

int num1,guess1;

int srand(time(NULL)); //seeds the generator

//generates a random number
num1 = rand() % 100 +1;

cout << "I'm thinking of a number from 1-100." <<endl;
cout << "Can you guess what it is? "<<endl;
cin >> guess1;



if(guess1 < num1)
{
while(guess1 != num1)
cout << "Too low.. try again." << endl;
cin >> guess1 ;
}

else if(guess1 > num1)
{
while(guess1 != num1)
cout <<"Too high.. try again." <<endl;
cin >> guess1;
}

else if(guess1 == num1)
{

cout << "Congratulations!!! You guessed the right number!";
cout << "The number was indeed " << num1 <<"."<<endl;

}

return 0;
}
What you need is something like this:


1
2
3
4
5
6
7
8
9
10
11
12
get initial guess

while (guess does not equal number)
{
     check if its too low

     check if its too high

     get next guess
}

output "success" message.


I'm just trying to show the sort of structure you need, obviously I've missed out all the detail.
(I'm still figuring out how to stop infinite loops)


An infinite loop would be something like this:
1
2
3
4
5
6
while (true) 
{
    cin >> guess1;
    if (guess1 == num1)
        break;
}


But that's not what I'm suggesting here. I'd prefer this:
1
2
3
4
while (guess1 != num1)
{
    cin >> guess1;
}


Again - I've simplified the code, this is just another outline.
closed account (3qX21hU5)
Also for copying code into forums they usually have a code format "[ code] and [ /code]" (Delete the spaces) that make the code much easier to read. Just a tip for future posts. Otherwise looks great keep it up man.
closed account (1R91hbRD)
Thank you chervil! I GOT IT! lots of help man! thank you for taking the time to explain it! when all hope was lost you showed me light! :)

It works fine however it only gives me the number 42 for my random number, and continues to give me 42 each time i run it. I was intending to make it give me a different number each time i ran the program. Help please!


side note: i'm trying to use the correct format, but i'm failing :( do i type /code then paste code?

note: brandon will do! thanks for the tip!
[#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
system("color C0"); //gives a appealing color to the background and symbols.

int num1,guess1;

int srand(time(NULL)); //seeds the generator

//generates a random number
cin.clear();
num1 = rand() % 100 +1;
cin.clear();
cout << "I'm thinking of a number from 1-100." <<endl;
cout << "Can you guess what it is? "<<endl;
cin >> guess1;



while(guess1 != num1)

{if(guess1 < num1)
{
cout << "Too low.. try again." << endl;
cin >> guess1;
}

else if(guess1 > num1)
{
cout <<"Too high.. try again." <<endl;
cin >> guess1;
}
}
cout << "Congratulations!!! You guessed the right number! ";
cout << "The number was indeed " << num1 <<"."<<endl;



return 0;
}
Last edited on
You aren't seeding the generator: look at your call to srand() and remove the int from in front of it.

The problem is that you're making an integer called srand instead of calling the function.
closed account (1R91hbRD)
Thank you! Its complete! great help! (you're not useless! :D)
That's good!

About the random number. Change this line:
int srand(time(NULL)); //seeds the generator
to this:
srand(time(NULL)); //seeds the generator
The first version is just a declaration, it tells the compiler the name of the function, and that it returns an integer. The seccond (corrected) version actually calls the function.

Regarding your code, it's fine no problems, well done.
But while we're here, sometimes it's worth looking if code can be shortened.
Start with this, which is ok:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (guess1 != num1)
{
    if (guess1 < num1)
    {
        cout << "Too low.. try again." << endl;
        cin >> guess1;
    }

    else if(guess1 > num1)
    {
        cout <<"Too high.. try again." <<endl;
        cin >> guess1;
    }
}


Though, the cin statement need be there just once.
... and, if the first test is false, the second must be true. If we also omit the braces, it reduces to this:
1
2
3
4
5
6
7
8
9
while (guess1 != num1) 
{
    if (guess1 < num1)
        cout << "Too low.. try again." << endl;
    else
        cout << "Too high.. try again." << endl;

    cin >> guess1;
}


Finally - the code formatting. When you are editing the post, click the <> button on the right.
Topic archived. No new replies allowed.