• Advertisement
Sign in to follow this  

Problem With Guessing Program [solved]

This topic is 4364 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

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Call

int randomnum = (random % 10) + 1;


in your while loop before cout.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement