Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- using namespace std;
- int choice, problems, problemsDone, i, num1, num2, correct;
- float userAnswer, actualAnswer, grade;
- double dividedAnswer;
- string name;
- char ch;
- void menu();
- int main()
- {
- cout << "Math Tutor \n";
- cout << "\n";
- cout << "What is your name? \n";
- getline(cin, name);
- cout << "Welcome, " << name << "!\n";
- menu();
- cin.get(ch);
- ch = cin.get();
- }
- void menu()
- {
- cout << "\n";
- cout << "Please choose one of the following options! \n";
- cout << "1. Addition \n";
- cout << "2. Subtraction \n";
- cout << "3. Multiplication \n";
- cout << "4. Division \n";
- cout << "5. Quit \n";
- while (true)
- {
- if (!(cin >> choice))
- {
- cout << "Input must be an integer. Please try again. \n";
- cin.clear();
- cin.ignore();
- menu();
- }
- switch (choice)
- {
- case 1: //addition
- cout << "How many problems would you like? \n";
- cin >> problems;
- if (problems > 1)
- {
- do
- {
- srand(time(0));
- num1 = rand() % 100;
- num2 = rand() % 100;
- cout << "What is " << num1 << "+" << num2 << "?\n";
- cin >> userAnswer;
- actualAnswer = num1 + num2;
- if (userAnswer == actualAnswer)
- {
- cout << "Good job! \n";
- ++correct;
- }
- else {
- cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
- }
- ++problemsDone;
- } while (problemsDone < problems);
- grade = (correct * 100) / problems;
- cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
- break;
- }
- else
- {
- cout << "Invalid entry. \n";
- menu();
- }
- break;
- case 2: //subtraction
- cout << "How many problems would you like? \n";
- cin >> problems;
- if (problems > 1)
- {
- do
- {
- srand(time(0));
- num1 = rand() % 100;
- num2 = rand() % 100;
- cout << "What is " << num1 << "-" << num2 << "?\n";
- cin >> userAnswer;
- actualAnswer = num1 - num2;
- if (userAnswer == actualAnswer)
- {
- cout << "Good job! \n";
- ++correct;
- }
- else {
- cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
- }
- ++problemsDone;
- } while (problemsDone < problems);
- grade = (correct * 100) / problems;
- cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
- }
- else {
- cout << "Invalid entry. \n";
- menu();
- }
- break;
- case 3: //multiplication
- cout << "How many problems would you like? \n";
- cin >> problems;
- if (problems > 1)
- {
- do
- {
- srand(time(0));
- num1 = rand() % 100;
- num2 = rand() % 100;
- cout << "What is " << num1 << "*" << num2 << "?\n";
- cin >> userAnswer;
- actualAnswer = num1 * num2;
- if (userAnswer == actualAnswer)
- {
- cout << "Good job! \n";
- ++correct;
- }
- else {
- cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
- }
- ++problemsDone;
- } while (problemsDone < problems);
- grade = (correct * 100) / problems;
- cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
- break;
- }
- else
- {
- cout << "Invalid entry. \n";
- menu();
- }
- break;
- case 4: //division
- cout << "How many problems would you like? \n";
- cin >> problems;
- if (problems > 1)
- {
- do
- {
- srand(time(0));
- num1 = rand() % 100 + 1;
- num2 = rand() % 100 + 1;
- cout << "What is " << num1 << "/" << num2 << "?\n";
- cin >> userAnswer;
- dividedAnswer = (double)num1 / num2;
- if (userAnswer == dividedAnswer)
- {
- cout << "Good job! \n";
- ++correct;
- }
- else {
- cout << "Incorrect, the correct answer is " << actualAnswer << ".\n";
- }
- ++problemsDone;
- } while (problemsDone < problems);
- grade = (correct * 100) / problems;
- cout << "You got a " << grade << "%. You got a total of " << correct << " correct out of " << problems << ".\n";
- break;
- }
- else {
- cout << "Invalid entry.\n";
- menu();
- }
- break;
- case 5: //quit
- cout << "Goodbye!" << endl;
- return;
- default: //handler
- cout << "Incorrect, please try again. \n \n";
- menu();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement