| | |
C++ Random Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
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;
}
#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;
}
C++ Syntax (Toggle Plain Text)
else if (guess = num)
should be:
C++ Syntax (Toggle Plain Text)
else if (guess == num)
•
•
•
•
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?

Aside from Clinton's excellent spot, there is a slight flaw in your logic...
C++ Syntax (Toggle Plain Text)
//Snipped #include's int main ( ) { int guess; char ans; srand (time (0)); for (int i = 0; i < 100; i++) {
C++ Syntax (Toggle Plain Text)
int random = rand( ); 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...
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- C++ Reorder random numbers (C++)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: aight i tried something new!
- Next Thread: error C2064: term does not evaluate to a function taking 1 arguments
Views: 3724 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





