#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void game();
void menu_Select();
int choice;
void game()
{
srand((unsignedint)time(NULL));
//declare variables
int min_num=0; int max_num=0;
int range = (max_num - min_num) + 1;
int rand_gen = rand() % range + min_num;
//game logo & intro
cout << "\t\tWelcome to GUESSING MAXIMUM AND MINIMUM GAME !!\n";
cout << "Please enter number between your range. Are you up to the challenge ?!\n";
cout << endl;
cout << "-----------------****GUESSING MAXIMUM AND MINIMUM GAME*****------------------------------" << endl;
while (true)
{
cout<<"Please key in the minimum value and maximum value for the range of the random number.\n";
cin >> min_num >> max_num;
cin.ignore();
//promt error message
while (max_num < min_num)
{
cout << "So sorry. Please re-enter your minimum and maximum value.\n";
cout << "Enter your value : ";
cin >> min_num >> max_num;
cin.ignore();
}
//check maximum and minimum value
if (rand_gen % 2 == 0 && rand_gen > 0)
{
cout << "The first number generated is " << rand_gen << " It is an even positive number" << endl;
}
else
{
cout << "The first number generated is " << rand_gen << " It is an odd positive number" << endl;
}
if (rand_gen % 2 == 0 && rand_gen < 0)
{
cout << "The first number generated is " << rand_gen << " It is an even negative number" << endl;
}
else
{
cout << "The first number generated is " << rand_gen << " It is an odd negative number" << endl;
}
if (rand_gen == 0)
{
cout << "The number generated is " << rand_gen << endl;
}
}
menu_Select();
}
void menu_Select()
{
do
{
cout << "*****QUESTION*****" << endl;
cout << "1-Play" << endl;
cout << "2-Exit" << endl;
cout << "Your choice : ";
cin >> choice;
cout << "\n\n";
} while (choice < 1 || choice > 2);
if (choice == 1)
{
game();
}
}
int main()
{
menu_Select();
return 0;
}
SAMPLE OUTPUT
Example 1
Please key in the minimum value and maximum value for the range of the random number:
-10 10
The first number generated is 7. It is an odd positive number.
The second number generated is -3. It is a negative number.
Example 2
Please key in the minimum value and maximum value for the range of the random number:
-10 20
The first number generated is 0.
The second number generated is 18. It is an even positive number.
//declare variables
int min_num=0; int max_num=0;
int range = (max_num - min_num) + 1;
int rand_gen = rand() % range + min_num;
So this part in lines 19-21, should just be int min_num, max_num;
(delete everything else)
Then around line 43 you want
1 2
int r1 = rand() % (max_num-min_num+1) + min_num;
int r2 = rand() % (max_num-min_num+1) + min_num;
then do your comparisons against r1 and r2
You were defining the range (1) and the random number (always 0) well in advance of the user input. Btw, are you planning to stop that infinite while loop somehow?
They're defined in your original line 32, from the user input, as you prob figured out, which happens before their usage in my suggestion (~line 43). Glad it works.
Here you have taken the wrong value which does not match according to your deserve value If you have found true result according to given input then you need to change in this int values as well as if conditional loop.
Here I have define the mistake which is puting in this code.
int range = (max_num - min_num) + 1;
int rand_gen = rand() % range + min_num;
if (rand_gen % 2 == 0 && rand_gen > 0)