For this project you will create a program that will test the user’s proficiency
ID: 3718406 • Letter: F
Question
For this project you will create a program that will test the user’s proficiency at solving different types of math problems. The program will be menu driven. The user will select either addition, subtraction, multiplication or division problems. The program will then display a problem, prompt the user for an answer and then check the answer displaying an appropriate message to the user whether their answer was correct or incorrect. The user will be allowed 3 tries at each problem. The program will also keep statistics on how many problems were answered correctly. At this point you should have a working copy of the program that displays practice programs for addition, subtraction, multiplication and division. This week you will complete the application by adding functions to allow the user to select problems of varying degrees of difficulty and also keep statistics on number of correct answers vs total number of problems attempted. So now let’s add the code for the remaining functions: We’ll start by inserting code to ask the user if they want to see problems that are easy, medium or difficult. We’ll do this by prompting them to enter ‘e’ for easy, ‘m’ for medium or ‘d’ for difficult and we’ll do this right after they have selected the type of problems they want. We will also add a validation loop that will check to see if they entered a correct response (e,m, or d). Easy – problems using numbers 0 thru 10 Medium – problems 11 thru 100 Difficult – problems 100 – 1000 Use an if statement to check the difficulty and generate the random numbers. Statistics: Create two variables ttlProblems and correctAnswers. Every time the user answers a problem add 1 to ttlProblems and every correct answer add 1 to correctAnswers. We’ll display the total number of problems and correct answers as well as the percent correct after the user has selected the exit option on the main menu. This will complete the course project. Test it thoroughly and have some one you trust test it also
Explanation / Answer
#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to return random message for correct answer
string returnRightMessage()
{
// Stores the message for correct answer
string messageRight[] = {"Very good!", "Excellent!", "Nice Work!", "Keep up the good work"};
// Generates random number between 0 and 3
int no = rand() % 3;
// Returns message at no index position
return messageRight[no];
}// End of function
// Function to return random message for wrong answer
string returnWrongMessage()
{
// Stores the message for correct answer
string messageWrong[] = {"No. Please try again", "Wrong. Try one more", "Don't give up!", "No. Keep trying"};
// Generates random number between 0 and 3
int no = rand() % 3;
// Returns message at no index position
return messageWrong[no];
}// End of function
// Function to display menu for difficulty level, accept user choice for level and return it
char menuLevel()
{
char choice;
// Displays menu
cout<<" E for Easy.";
cout<<" M for Medium.";
cout<<" D for Difficult.";
cout<<" Q for Quit.";
cout<<" Enter the level: ";
// Accepts choice
cin>>choice;
// Returns choice
return choice;
}// End of function
// Function to display menu for arithmetic operation choice, accept user choice and return it
int menuArithmatic()
{
int choice;
// Displays menu
cout<<" 1 for Addition.";
cout<<" 2 for Subtraction.";
cout<<" 3 for Multiplication.";
cout<<" 4 for Division.";
cout<<" 5 for Exit.";
cout<<" Enter your choice: ";
// Accepts choice
cin>>choice;
// Returns choice
return choice;
}// End of function
// Function to generate random number and returns it based on the selected level
int randomNumber(char level)
{
// Checks if the level is one
if(level == 'e' || level == 'E')
// Generate and return random number between 1 and 9
return ((rand() % 9) + 1);
// Checks if level is two
else if(level == 'm' || level == 'M')
// Generate and return random number between 1 and 99
return ((rand() % 99) + 11);
// Otherwise, for arithmetic operation option 5 selected for random arithmetic operation
else if(level == 'd' || level == 'D')
// Generate and return random number between 1 and 4
return ((rand() % 999) + 100);
}// End of function
// Function to calculate percentage and return it
float claculatePercentage(int correct)
{
return ((correct / 10.0f) * 100);
}// End of function
// Function to generate question
void generateQuestion(int no1, int no2, int type)
{
// Checks the type of question
switch(type)
{
case 1:
cout<<no1<<" add "<<no2<<"? ";
break;
case 2:
cout<<no1<<" subtract "<<no2<<"? ";
break;
case 3:
cout<<no1<<" times "<<no2<<"? ";
break;
case 4:
cout<<no1<<" divide "<<no2<<"? ";
break;
}// End of switch - case
}// End of function
// main function definition
int main()
{
// Local variable to store data
int c = 0, type;
int correctCount = 0, wrongCount = 0;
float percent, userResult, computerResult, no1, no2;
// Loops till user choice is not 3 or 6
do
{
// Calls the function to select difficulty level
switch(menuLevel())
{
case 'e':
case 'E':
srand(c);
// Calls the function to generate random number for level one i.e., single digit
no1 = randomNumber('e');
no2 = randomNumber('e');
break;
case 'm':
case 'M':
srand(c);
// Calls the function to generate random number for level two i.e., double digit
no1 = randomNumber('m');
no2 = randomNumber('m');
break;
case 'd':
case 'D':
srand(c);
// Calls the function to generate random number for level two i.e., double digit
no1 = randomNumber('d');
no2 = randomNumber('d');
break;
case 'q':
case 'Q':
cout<<" Your Score: ";
// Calculates the percentage
percent = claculatePercentage(correctCount);
cout<<" Correct Answers: "<<correctCount;
cout<<" Wrong Answers: "<<wrongCount;
cout<<" Percentage: "<<percent<<"%";
exit(0);
default:
cout<<" Invalid choice!";
}// End of inner switch case
// Calls the function to check type of arithmetic operation
switch(menuArithmatic())
{
case 1:
// Calls the function to generate question
generateQuestion(no1, no2, 1);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 + no2;
break;
case 2:
// Calls the function to generate question
generateQuestion(no1, no2, 2);
cin>>userResult;
computerResult = no1 - no2;
break;
case 3:
// Calls the function to generate question
generateQuestion(no1, no2, 3);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 * no2;
break;
case 4:
// Calls the function to generate question
generateQuestion(no1, no2, 4);
// Accept result from the user
cin>>userResult;
// Calculates the result
computerResult = no1 / (float)no2;
break;
case 6:
// Calculates the percentage
percent = claculatePercentage(correctCount);
cout<<" Your Score: ";
cout<<" Correct Answers: "<<correctCount;
cout<<" Wrong Answers: "<<wrongCount;
cout<<" Percentage: "<<percent<<"%";
exit(0);
default:
cout<<" Invalid choice!";
}// End of outer switch case
// Checks the result of user and computer if both are same
if(userResult == computerResult)
{
// Increase the correct answer counter
correctCount++;
// Displays randomly generated correct answer message
cout<<returnRightMessage();
}// End of if condition
// Otherwise wrong answer given by the user
else
{
// Increase the wrong answer counter
wrongCount++;
// Displays randomly generated wrong answer message
cout<<returnWrongMessage();
}// End of else
// Increase the number of question generated
c++;
// Checks if number of question is 10
if(c == 10)
{
// Calculates the percentage
percent = claculatePercentage(correctCount);
// Checks if he percentage is less than 75% display message
if(percent < 75)
cout<<" Please ask your teacher for extra help. ";
// Otherwise, percentage is greater than or equals to 75% display message
else
cout<<" Congratulations you are ready to go to the next level! ";
cout<<" Correct Answers: "<<correctCount;
cout<<" Wrong Answers: "<<wrongCount;
cout<<" Percentage: "<<percent<<"%";
// Reset the counters to zero
correctCount = wrongCount = c = 0;
}// End of if condition
}while(1); // End of do - while loop
}// End of main function
Sample Output:
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: e
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 1
3 add 7? 10
Excellent!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: e
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 2
6 subtract 9? -3
Excellent!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: e
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 3
1 times 3? 3
Very good!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: E
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 4
4 divide 6? 1
No. Please try again
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: M
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 1
62 add 37? 99
Very good!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: M
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 2
65 subtract 93? -28
Very good!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: m
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 3
69 times 51? 3519
Nice Work!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: m
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 4
72 divide 108? 12
Don't give up!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 1
164 add 298? 462
Nice Work!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 2
168 subtract 257? -123
Wrong. Try one more
Please ask your teacher for extra help.
Correct Answers: 7
Wrong Answers: 3
Percentage: 70%
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: e
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 1
3 add 7? 10
Excellent!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 1
141 add 585? 726
Excellent!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 2
145 subtract 345? -200
Very good!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 3
148 times 303? 44844
Very good!
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: D
1 for Addition.
2 for Subtraction.
3 for Multiplication.
4 for Division.
5 for Exit.
Enter your choice: 4
151 divide 1062? 1
No. Please try again
E for Easy.
M for Medium.
D for Difficult.
Q for Quit.
Enter the level: Q
Your Score:
Correct Answers: 4
Wrong Answers: 1
Percentage: 40%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.