Sign in to follow this  
Pharaoh12

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this