November 21st, 2013, 04:25 PM

Random number generator problem(noob)
Code:
/*
Sean M. Dane
November 14,2013
COP 2000 assignment #4
*/
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
void intRand (int *);
void showMenu();
int main ()
{
float guess, ANS;
int r[2];
int choice = 0;
int ADD_CHOICE = 1,
QUIT_CHOICE = 4;
srand((unsigned int)time((time_t *)NULL));//need for rand()
showMenu();
while (choice < ADD_CHOICE  choice > QUIT_CHOICE)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
if (choice == QUIT_CHOICE)
return 0;
cout << "What is your answer for ";
intRand(r);
switch (choice)
{
case 1:
ANS = (r[0] + r[1]);
cout << r[0] + r[1] << "= ? ";
cin>> guess;
if (guess == ANS)
cout << "Great job!";
else
cout << ANS << '\n';
break;
case 2:
ANS = (r[0]  r[1]);
cout << (r[0]  r[1]) << "= ? ";
cin>> guess;
if (ANS == guess)
cout << "Great job!";
else
cout << ANS << '\n';
break;
case 3:
ANS = (r[0] * r[1]);
cout << r[0] * r[1] << "= ? ";
cin>> guess;
if (ANS == guess)
cout << "Great job!";
else
cout << ANS << '\n';
break;
}
return 0;
}
void showMenu()
{
cout << "\n\t\tMENU OF OPERATIONS \n\n"
<< " 1. Addition.\n"
<< "\n 2. Subtraction.\n"
<< "\n 3. Multiplication.\n"
<< "\n 4. Quit the Program.\n\n";
}
void intRand(int *r)
{
r[0] = rand() % 11 + 2;
r[1] = rand() % 11 + 2;
}
it seems to be generating 1 number and not 2. when I click 1 for addition it says
what is your answer for 9= ? =
November 21st, 2013, 05:23 PM

You could try to output r[0] and r[1] in the intRand function.
But my guess is that it is calculated when you try to print/cout it out.
Take a look at how you get the ANS value, and then look how you want to print it out. (same syntax used)
To print each out, try something like this (untested):
Code:
cout << r[0] << " + " << r[1] << "= ? ";
November 21st, 2013, 05:28 PM

(guess == ANS)
This is binary floatingpoint that we're talking about, so the representation of decimal values is only approximate. It may look like they'd have to be equal, but they could instead differ by less than a millionth  remember, they have to be exactly the same to count as being equal.
Why use float? The numbers you are getting in intRand are integers. Why not work with integers instead? Integers won't give the approximation problems that floatingpoint will.
November 21st, 2013, 05:57 PM

BTW, compiling your code asis and running, I got this:
Code:
C:TEST>a
MENU OF OPERATIONS
1. Addition.
2. Subtraction.
3. Multiplication.
4. Quit the Program.
Please enter a valid menu choice: 1
What is your answer for 15= ? 15
Great job!
C:TEST>
So what problem do you think exists? What makes you think that one exists?
Last edited by dwise1_aol; November 21st, 2013 at 06:21 PM.