I am having a bit of trouble with some code I have recently written. It runs without errors right now, but it needs to do a few more things that i have yet to figure out.
The program asks the user for the answers to 4 mathematical questions whose inputs are all between 10 and 99. one problem is addition, one subtraction, one multiplication, and one divison.
The program displays a message telling the user if they answered incorrectly, and gives them another shot at the same problem.
The program is supposed to display one of the 3 random congratulatory message if the user gets the answer correct. **I am stumped on this one so far.**
The messages are:
“Nice Job!"
“Congratulations! Your answer is correct.”
“Yes! You are right!”
So far I have it set to just display "Nice!"
Here it is so far:
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
|
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
srand(time(NULL));
int a = rand()%(99-10+1) + 10;
int b = rand()%(99-10+1) + 10;
int c = rand()%(99-10+1) + 10;
int 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;
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<< "\n Nice!\n\n";
}
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<< "\n Nice!\n\n";
}
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<< "\n Nice!\n\n";
}
cout<<" "<<g<<"/"<<h<< " = ";
cin>>value_4;
while (value_4 <= (g/h)-.009 || value_4 >= (g/h)+.009)
{
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<< "\n Nice!\n\n";
}
return 0;
}
|
I am having some issues with the division problem as well. The users answer is supposed to be okay as long as the answer is correct out to 2 decimal places.
The way I have the program now, issues could arise...
take for instance a problem 56/47 whose answer is approx. 1.1914894
With the code I have, the answer 1.185 would be considered correct because it lies within -.009 of the actual answer. This however should not be the case, for the users answer should be accurate to 2 decimal places.