Problem With Guessing Program [solved]

Recommended Posts

Sorry to come here with yet another problem but ive been debugging for almost an hour and cant find the problem!Im trying to make a program where the user picks a number and then the program keeps guessing until it guesses right.But I can only get it to guess the same number everytime so the program gets caught in an infinite loop. Here is my code: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int thenum; cout << "Type in a number from 1 to 10 for me to guess:\n"; cin >> thenum; srand(time(0)); int random = rand() % 10 + 1; int randomnum = (random % 10) + 1; cout << "Is it " << randomnum << "?"; while (randomnum != thenum) { cout << "Then is it " << randomnum; } cout << "I GOT IT!"; system("pause"); return 0; } Thank you for your help! -Pharaoh12 [Edited by - Pharaoh12 on March 13, 2006 9:46:40 PM]

Share on other sites
You have to call rand() each time you want a new random number. randomnum is going to remain the same until you actually change it.

CM

Share on other sites
Call

int randomnum = (random % 10) + 1;

in your while loop before cout.

Share on other sites
Conner is right, you need the rand in the while loop. Something like below. That way a new number is randomly selected each time through the loop.

Greig

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int thenum;
cout << "Type in a number from 1 to 10 for me to guess:\n";
cin >> thenum;

srand(time(0));

int randomnum = rand() % 10 + 1;

cout << "Is it " << randomnum << "?";

while ((randomnum != thenum)
{
randomnum = rand() % 10 + 1;
cout << "Then is it " << randomnum;
}
cout << "I GOT IT!";
system("pause");
return 0;
}

Share on other sites
I see what your saying but it still doesnt work. The compiler still says that there is something wrong with the opening statment of the while loop.

Share on other sites
Can you show us the error message you are seeing. Also you say you are "still" getting the same error message. You never mentioned an error message before.

Also post your new code. so we can see what you now have.

Greig

Share on other sites

while ((randomnum != thenum)

Your problem is that you have 2 (. You should only have one.

Should be:

while (randomnum != thenum)

Share on other sites
Thanks Dorvo for pointing that out. Yes please when copying my code remove one of the ('s at the start of the loop.

Greig

Share on other sites
Quote:
 Original post by Greig HamiltonThanks Dorvo for pointing that out. Yes please when copying my code remove one of the ('s at the start of the loop.Greig

No problem. I can tell you without a doubt that you're not the only person to overlook small things like a typo. [smile]

Share on other sites
OK, it turns out Dorvo figured out the problem. There were 2 opening parenthesis " (( " when there should have been 1.Thanks a lot everybody, the program is running clean and I added some stuff to make it look better.Here is my concluding code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int thenum;
cout << "Type in a number from 1 to 10 for me to guess:\n";
cin >> thenum;

srand(time(0));
int random = rand();
int randomnum = (random % 10) + 1;
int tries = 0, guess;

cout << "Is it " << randomnum << "?\n";
++tries;

while (randomnum != thenum)
{

randomnum = rand() % 10 + 1;
cout << "Then is it " << randomnum << ".\n";
++tries;
if (tries > 9)
{
cout << "I cant figure it out!";
}
}
cout << "I GOT IT!I tried " << tries << " times.";
system("pause");
return 0;
}

THANKS A LOT EVERYONE!

-Pharaoh12

Create an account

Register a new account

• Forum Statistics

• Total Topics
628294
• Total Posts
2981876

• 11
• 10
• 10
• 11
• 17