Why is the output of this wrong?
It doesn't output the proper number of pseudorandom numbers. Well, sometimes it does and sometimes it doesn't.
One example can be seen when you enter 9 at the first prompt and 8 at the second. Or 8 at the first and 3 at the second.
Code:
#include <iostream> //opens iostream as a standard C++ included file
#include <conio.h> //opens conio.h for use of getch() and system() commands
#include <ctime> //opens ctime for the seeding of rand() using time
#include <cstdlib> //opens cstdlib for rand() function
#include <algorithm> //opens algorithm for sort() function
#include <set> //opens set for the use of sets
using namespace std; //uses the standard C++ namespace
int main() //opens the main (and only) function
{
int maxnum; //sets the variable "maxnum" (the highest random number that will be displayed) as an integer
int numofnums; //sets the variable "numofnums" (the number of random numbers that will be displayed) as an integer
int watcher; //sets the variable "watcher" (counter) as in integer
int endopt; //sets the variable "endopt" (option to repeat the program or exit the program) as in integer
int i; //sets the variable i as an integer, but i is gone now, I think
int randnum; //sets the variable "randnum" (used to add a random number into the set) to and integer
srand(time(0)); //seeds rand() by using the system time; exact outcome is based on OS
cout << "Welcome to the Random Number Generator." << endl;
cout << "Press any key to continue." << endl;
getch(); //pauses the program until the user hits any key
system("cls"); //a system call to clear the screen
step1:
maxnum = 0;
numofnums = 0;
watcher = 0;
endopt = 0;
i = 0;
randnum = 0;
cout << "Enter the maximum number in your range of random numbers." << endl;
cout << "This number will be the highest possible number that can be output." << endl;
cin >> maxnum;
while (maxnum <= 1) //an error check for an invalid maximum number (maxnum)
{
system("cls"); //a system call to clear the screen
cout << "\aYou entered an invalid number." << endl;
cout << "The highest possible number must be greater than 1." << endl;
cout << "Please try again. Enter the highest possible random number." << endl;
cin >> maxnum;
}
system("cls"); //a system call to clear the screen
cout << "Enter the number of random numbers to display." << endl;
cout << "This will be the number of random numbers that you will see." << endl;
cin >> numofnums;
while (numofnums <= 0) //an error check for an invalid number of random numbers
{
system("cls"); //a system call to clear the screen
cout << "\aYou entered an invalid number." << endl;
cout << "The number of displayed numbers must be greater than 0." << endl;
cout << "Please try again. Enter the number of random numbers to display." << endl;
cin >> numofnums;
}
if (numofnums >= maxnum)
{
cout << "You entered invalid numbers." << endl;
cout << "To prevent double or triple numbers, the range of the random numbers \n must be greater than the total number of random numbers displayed." << endl;
cout << "Press any key to try again." << endl;
getch(); //a "pause" command
system("cls"); //a system call to clear the screen
goto step1; //brings the user back to the beginning to try again...might replace this goto in the future
}
else
{
set<int, less<int> > randSet; //creates the set
watcher = 0; //sets the counter to 0 to prevent problems
while (watcher < (numofnums))
{
randnum = (rand() % (maxnum + 1));
randSet.insert(randnum); //fills the set with values
watcher = watcher + 1; //adds one to "watcher", which is the counter
}
cout << "Your random numbers are..." << endl;
set<int>::iterator iter;
for (iter = randSet.begin(); iter != randSet.end(); iter++)
cout << *iter << endl;
do
{
cout << "\n"; //newline
cout << "Enter 1 to run again or 2 to exit." << endl;
cin >> endopt;
system("cls"); //a system call to clear the screen
switch (endopt)
{
case 1:
goto step1;
break;
case 2:
return 0;
break;
}
}
while (endopt != 1 && endopt != 2);
}
}