944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4862
  • C++ RSS
Mar 18th, 2006
0

C++ Random Numbers

Expand Post »
Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?

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

int main ( )
{
int guess;
char ans;

srand (time (0));
for (int i = 0; i < 100; i++)
{
int random = rand( );
int num = ((random % 100) + 1);



cout << "Guess a number between 1 and 100: ";
cin >> guess;

if (guess > num)
{
cout << "The number is lower. Try Again!!\n\n";
continue;

}
if (guess < num)
{
cout << "The number is higher. Try Again!!\n\n";
continue;


}
else if (guess = num)
{
cout << "You've guessed correctly!\n\n";
break;
}

}



return 0;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Duke_0064 is offline Offline
6 posts
since Mar 2006
Mar 18th, 2006
0

Re: C++ Random Numbers

C++ Syntax (Toggle Plain Text)
  1. else if (guess = num)

should be:
C++ Syntax (Toggle Plain Text)
  1. else if (guess == num)
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Mar 18th, 2006
0

Re: C++ Random Numbers

Quote originally posted by Duke_0064 ...
Hi, I'm new to this C++ stuff. So if anyone can help that would be nice. I have this program to generate a random number and a the user inputs a number from the keyboard to correctly guess the number that the computer randomly chooses. but the numbers are always changing each time, which makes it hard to correctly guess the correct number. So the question is, is it possible to randomly choose a number without it changing each iteration?
Wrap your code in "Code Tags" - makes it easier for everyone to read.

Aside from Clinton's excellent spot, there is a slight flaw in your logic...

C++ Syntax (Toggle Plain Text)
  1. //Snipped #include's
  2. int main ( )
  3. {
  4. int guess;
  5. char ans;
  6.  
  7. srand (time (0));
  8. for (int i = 0; i < 100; i++)
  9. {
So... everything after this curly brace (up til the closing brace) gets repeated 100 times.. including:
C++ Syntax (Toggle Plain Text)
  1. int random = rand( );
  2. int num = ((random % 100) + 1);

I suggest putting these 2 lines before your loop starts, then you will only be selecting your random number once.


Also, this kind of "guessing game" would look much cleaner if you could say do....while(guess!=num) instead of for...
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Mar 18th, 2006
0

Re: C++ Random Numbers

Thanks guys!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Duke_0064 is offline Offline
6 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: aight i tried something new!
Next Thread in C++ Forum Timeline: error C2064: term does not evaluate to a function taking 1 arguments





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC