Random sign

  • Thread starter Thread starter John
  • Start date Start date
J

John

I'm creating this program to test an elementary
school kids math skills. They have five options to
choose from

1 = '+'
2 = '-'
3 = '*'
4 = '/'
0 = To have the computer choose.
My Problem is that when you choose " 0 " the
randomization doesn't work consistently. The computer
does choose the mathematical sign but it looses its
random consistancy, for example; the uwer chooses 0 ( to
let the computer choose the mathematical sign ) and this
is what it would look like:
6 * 6 =
6 * 9 =
9 * 5 =
4 * 7 = etc......
Now, this is what it SHOULD look like:
9 - 4 =
5 * 7 =
4 + 5 =
6 + 8 = ....etc
Sorry if the code is a little messy, I didn't know
how to attach a message, so I copied it straight from the
file.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int Random_num ( void );
int main()
{

unsigned int counter,
number1, number2,
panswer,
R_answer,
choice,
point;
double final_score;
enum { add = 1, subtract, multiply, division };

cout << " Welcome to the Elementary Math Help. This
program will test your n/"
<< " skills in Addition, Subtraction,
Multiplication, and Division."<< endl;
cout << " Choose 1 for addition \n"
<< " Choose 2 for subtraction \n"
<< " Choose 3 for multiplication \n"
<< " Choose 4 for Division \n "
<< " or press 0 for the computer to choose \n";
cin >> choice;
counter = 0;
point = 0;
srand( time (0) );
if ( choice == 0 )
choice = rand()% 3 + 1;
while ( counter != 5 ){


R_answer = 0;

panswer = 0;

number1 = Random_num();

number2 = Random_num();

switch ( choice ){

case add:

R_answer = number1 + number2;

cout << number1 << " + " << number2 << " = ";

counter++;

cin >> panswer;

break;

case subtract:

R_answer = number1 - number2;

cout << number1 << " - " << number2 << " = ";

counter++;

cin >> panswer;

break;

case multiply:

R_answer = number1 * number2;

cout << number1 << " * " << number2 << " = ";

counter++;

cin >> panswer;

break;

case division:

R_answer = number1 / number2;

cout << number1 << " / " << number2 << " = ";

counter++;

cin >> panswer;

break;

default: cout << " Program Bug. ";

break;

}

if ( panswer == R_answer )

point++;
}
final_score = static_cast <double> ( point ) / 2;
if ( final_score <= .75 )
cout << " You scored low, please try again. " << endl;

cout << "Your score is " << setprecision (2)
<< setiosflags ( ios :: fixed | ios :: showpoint )
<< final_score << endl;
return 0;
}
int Random_num ( void )
{
int num;
num = 1 + rand() % 9;

return num;
}
 
John said:
My Problem is that when you choose " 0 " the
randomization doesn't work consistently. The computer
does choose the mathematical sign but it looses its
random consistancy, for example; the uwer chooses 0 ( to
let the computer choose the mathematical sign ) and this
is what it would look like:
srand( time (0) );
if ( choice == 0 )
choice = rand()% 3 + 1;

You should only seed the random number generator (srand) once, not every time
you choose. But that's not the problem...
if ( choice == 0 )
choice = rand()% 3 + 1;
while ( counter != 5 ){

You're choosing the random sign outside your while loop. Therefore it only
gets randomly set once. You want to set it inside the loop so it changes each
time through the loop.

Ken
 
Thank you for your help but I have already tried to
put the if statement in the while loop. and it does the
same exact thing. So what do you think

P.S

I forgot to mention, and I think think it's quite
obvious, that this is a win32 project. Also, I'm not
allowed to use pointers or arrays yet or anything beyond
functions.
 
Back
Top