Problem With Guessing Program [solved]

Started by
8 comments, last by Pharaoh12 18 years, 1 month ago
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]
Advertisement
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
Call
int randomnum = (random % 10) + 1;

in your while loop before cout.
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;
}
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.
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
while ((randomnum != thenum)


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

Should be:
while (randomnum != thenum)
Thanks Dorvo for pointing that out. Yes please when copying my code remove one of the ('s at the start of the loop.

Greig
Quote:Original post by Greig Hamilton
Thanks 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]
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

This topic is closed to new replies.

Advertisement