Hello all, I recently posted a thread concerning issues with using rand in programs.
I am still having some trouble as I now have to change the program again.
The program outputs 4 problems for the user to complete and displays a congratulatory message if the user got the answer correct, as well as giving a message telling the user if they got it wrong and gives them another chance at the problem until they get it right.
Now I need the program to output 20 problems with not only random integers between 10-99, but I also need it to give a random operand between *,/,+,-.
Here is the program now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
srand(time(NULL));
double a = rand()%(99-10+1) + 10;
double b = rand()%(99-10+1) + 10;
double c = rand()%(99-10+1) + 10;
double d = rand()%(99-10+1) + 10;
double e = rand()%(99-10+1) + 10;
double f = rand()%(99-10+1) + 10;
double g = rand()%(99-10+1) + 10;
double h = rand()%(99-10+1) + 10;
double value_1, value_2, value_3, value_4;
static const char* answers[] = {"\nNice Job!\n\n" , "\nCongratulations! Your answer is correct.\n\n", "\nYes! You are right!\n\n"};
cout<< "This program will test your arithmetic skills.\n \n";
cout<<" "<<a<<"+"<<b<< " = ";
cin>>value_1;
while (value_1 != (a+b))
{
cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
cout<< " "<<a<<"+"<<b<< " = ";
cin>> value_1;
}
if (value_1 == (a+b))
{
cout << answers[rand() % 3];
}
cout<<" "<<c<<"-"<<d<< " = ";
cin>>value_2;
while (value_2 != (c-d))
{
cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
cout<< " "<<c<<"-"<<d<< " = ";
cin>> value_2;
}
if (value_2 == (c-d))
{
cout << answers[rand() % 3];
}
cout<<" "<<e<<"*"<<f<< " = ";
cin>>value_3;
while (value_3 != (e*f))
{
cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
cout<< " "<<e<<"*"<<f<< " = ";
cin>> value_3;
}
if (value_3 == (e*f))
{
cout << answers[rand() % 3];
}
cout<<" "<<g<<"/"<<h<< " = ";
cin>>value_4;
while (value_4 <= (g/h)-.01 || value_4 >= (g/h)+.01)
{
cout<< "Sorry, wrong answer. Please try to enter the correct answer again. \n\n";
cout<< " "<<g<<"/"<<h<< " = ";
cin>> value_4;
}
if (value_4 >= (g/h)-.009 && value_4 <= (g/h)+.009)
{
cout << answers[rand() % 3];
}
return 0;
}
|
Can you use rand to choose between 4 options that are not integers? Or is there a way I can use rand to choose between integers that are assigned to each individual operand?
Finally, Is there a way I can just use a and b as my variables and have them be assigned different numbers each time they are used instead of using 40 different variables like a,b,c,d,e....