How do I change this code so that the user can choose a math operation and determine have that type of operation(either add sub mult or divide) run as much as a user wants it to? Say, if the user wanted 5 addition questions only.
And how do I keep track of the correct answers that they get?
Code:
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- #include<iomanip>
- #include<fstream>
- usingnamespace std;
- // Function prototypes
- int getuserchoice();
- int getNumQuestions();
- int getHighestNum();
- void showmenu();
- void showcorrectanswer(int,int);
- //Variables
- int previoususer;
- int number1, number2;
- int numquestions;
- int highestNum;
- char username;
- int userchoice;
- int studentanswer;
- float correctanswer;
- string newuser;
- // Variables to keep track of scores
- int userscore =0;
- //Constants for the menu choices
- constint ADDITION_CHOICE =1,
- SUBTRACTION_CHOICE =2,
- MULTIPLICATION_CHOICE =3,
- DIVISION_CHOICE =4,
- QUIT_CHOICE =5;
- int main()
- {
- cout <<"This program will display a menu of math operations. You will enter your name then choose an option for the menu. The program will save your scores and keep track of it. Let's get started!\n\n"<< endl;
- cout <<"The previous user is: "<< previoususer << endl;
- cout << endl;
- cout <<"What is your name?"<< endl;
- cin >> newuser;
- cout <<"Hi, "<< newuser << endl;
- // Get userchoice
- userchoice = getuserchoice();
- // Get number of questions
- numquestions = getNumQuestions();
- // Get highest number
- highestNum = getHighestNum();
- //Write name to file
- ofstream filenames;
- filenames.open("nameuser.txt");
- filenames << newuser;
- filenames.close();
- cout << newuser;
- // Show menu
- showmenu();
- return0;
- }
- //GET USERCHOICE FUNCTION
- int getuserchoice()
- {
- cout <<"Choose from the following options:\n\n"
- <<"1. Addition\n"
- <<"2. Subtraction\n"
- <<"3. Multiplication\n"
- <<"4. Division\n"
- <<"5. Quit\n\n"
- <<"Enter a number from 1 to 5 and hit Enter: \n";
- cin >> userchoice;
- return userchoice;
- }
- //NUMBER OF QUESTIONS FUNCTION
- int getNumQuestions()
- {
- cout <<"How many questions do you want to answer? "<< endl;
- cin >> numquestions;
- return numquestions;
- }
- // GET HIGHEST NUM FUNCTION
- int getHighestNum()
- {
- cout <<"What is the highest number that you want in your questions?"<< endl;
- cin >> highestNum;
- return highestNum;
- }
- void showmenu()
- {
- do
- {// Beggining of do-while loop
- unsignedint number1 =0;
- unsignedint number2 =0;
- srand(unsigned(time(0)));
- number1 =1+rand()% highestNum;
- number2 =1+rand()% highestNum;
- // Validates the input (must not be less than 1/greater than 5)
- if(userchoice < ADDITION_CHOICE || userchoice > QUIT_CHOICE)
- {
- cout <<"Please choose a valid number from 1 to 5\n\n"
- <<"1. Addition\n"
- <<"2. Subtraction\n"
- <<"3. Multiplication\n"
- <<"4. Division\n"
- <<"5. Quit\n\n"
- <<"Enter your choice: ";
- cin >> userchoice;
- }
- if(userchoice ==5){
- cout <<"goodbye"<< endl;
- }
- //Setting the decimal to two decimal places.
- cout <<setprecision(2)<< fixed << endl;
- // Respond to user's choice
- switch(userchoice)
- {// Addition
- case ADDITION_CHOICE:
- cout <<"Solve the problem below: \n";
- cout << number1 <<" + "<< number2 <<" = "<< endl;
- correctanswer = number1 + number2;
- cin >> studentanswer;
- if(studentanswer == correctanswer)
- cout <<"Correct! Congratulations.\n";
- else
- cout <<"That is incorrect. The correct answer is "<< correctanswer << endl;
- break;
- // Subtraction
- case SUBTRACTION_CHOICE:
- cout <<"Solve the problem below: \n";
- cout << number1 <<" - "<< number2 <<" = "<< endl;
- correctanswer = number1 - number2;
- cin >> studentanswer;
- if(studentanswer == correctanswer)
- cout <<"Correct! Congratulations.\n";
- else
- cout <<"That is incorrect. The correct answer is "<< correctanswer << endl;
- break;
- // Multiplication
- case MULTIPLICATION_CHOICE:
- cout << number1 <<" * "<< number2 <<" = "<< endl;
- correctanswer = number1 * number2;
- cin >> studentanswer;
- if(studentanswer == correctanswer)
- cout <<"Correct! Congratulations.\n";
- else
- cout <<"That is incorrect. The correct answer is "<< correctanswer << endl;
- break;
- // Division
- case DIVISION_CHOICE:
- cout <<"Round your answer to two decimal places."<<endl;
- cout << number1 <<" / "<< number2 <<" = "<< endl;
- correctanswer =(float)number1 / number2;// typecast float
- cin >> studentanswer;
- if(studentanswer<correctanswer+0.05&& studentanswer>correctanswer-0.05)
- cout <<"Correct! Congratulations.\n";
- else
- cout <<"That is incorrect. The correct answer is "<< correctanswer << endl;
- break;
- // Exit
- case QUIT_CHOICE:
- exit(0);
- break;
- // Default
- default: cout <<"you didn't enter a valid number;
- }
- }while(userchoice !=5)