### Help with math tutor

hey guys I'm writing a math tutor program and i need it to keep track of the number of correct scores as well as number tries and i also want to have 5 answer options,and also give the user up to 3 tries to get a question right. i have the basis of the code but the others elude me.
I'm also trying to write this without any global variables. thanks.
here's what i have so far
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283`` ``````#include #include #include #include #include using namespace std; char Menu(); //prototypes void difficulty(int &); void generate(double &, double &, int ); void addition(double &, double, double); void subtraction(double &, double, double); void multiplication(double &, double, double); void division(double &, double, double, char); void Ans(double &); int main() { double correctans, num1, num2; char choice; char line[80]; int diff; time_t now = time(0); string name; srand(now); // Seed the random number generator. //cout << ctime(&now) << endl; cout << "what is your first and last name " << endl; getline(cin, name); for (unsigned i = 0; i < name.length(); i++) if (name[i] == ' ') name[i] = '_'; name += ".txt"; cout << endl << endl << endl; cout << "Welcome to the Math Skills Program " << endl; cout << "This program allows you to practice your math skills" << endl; cout << "Choose what to practice in the menu shown below." << endl; cout << endl << endl << endl; difficulty( diff); bool test = true; while (test) { now = time(0); // cout << endl << "Problem start time:" << ctime(&now) << endl; generate(num1, num2, diff); choice = Menu(); switch (choice) { case 'a': addition(correctans, num1, num2); break; case 'b': subtraction(correctans, num1, num2); break; case 'c': multiplication(correctans, num1, num2); break; case 'd': division(correctans, num1, num2, choice); break; case 'q': test = false;// The user chose to quit the program. cout << "Thank you for using Math Tutor program.\n\n"; return 0; break; } } if (choice != 'q') { cout << "incorect input please try again" << endl; cin >> choice; // Loop again } return 0; } char Menu() { char choice; { cout << "Arithmetic Practice program\n" << endl; // Display the menu and get a choice. //cout << "------------------------------\n"; cout << "---------------------------------------------------------" << endl; cout << "ARITHMETIC PRACTICE PROGRAM " << endl; cout << "MAIN MENU" << endl; cout << "-------------------------------------------------------- - " << endl; cout << "a. Addition \n"; cout << "b. Subtraction\n"; cout << "c. Multiplication\n"; cout << "d. Division\n"; cout << "q. Quit the program\n"; cout << "------------------------------\n"; cout << "Enter your choice (a b c d q): "; cin >> choice; } // Validate the choice. choice = tolower(choice); if (choice >= 'a' && choice <= 'd' || choice == 'q') cout << "You entered: '" << choice << "', your input is valid " << endl; else { cout << "The valid choices are a, b, c, " << "d, and q. Please choose: "; cin >> choice; } cout << endl; return choice; } void difficulty(int & diff) { //double studentans; char line[80]; cout << "Select diffculty. Please choose" << endl; cout << " 1 for 1 digit\n" << " 2 for 2 digits\n" << " 3 for 3 digits\n" << " 4 for 4 digits\n" << " 5 for 5 digits\n" << endl; cout << endl << "> "; cin >> line; diff = atoi(line); while (diff < 1 || diff >5) { cout << "Enter 1-5 only " << endl; cin >> line; diff = atoi(line); cin.clear(); cin.ignore(1000, '\n'); } } void generate( double & num1, double & num2, int diff) { int min, max; min = pow(10., diff - 1); max = pow(10., diff); num1 = min + rand() % (max - min); num2 = min + rand() % (max - min); } void Ans(double & correctans) { double studentans; // char choice; // if (choice >= 'a' && choice <= 'd') // { cin >> studentans; if (studentans == correctans) cout << "\n\nCongratulations! That's right.\n\n"; else cout << "\n\nSorry, the correct answer is " << correctans << ".\n\n"; // } } void addition(double & correctans, double num1, double num2) { double studentans; //Generate two random numbers in the range 1 - 500. cout << "Prepare to add" << endl; //difficulty(num1, num2); // num1 and num2 haven't been randomly generated yet correctans = num1 + num2; // Display the problem. cout << "\n\n"; cout << " " << setw(4) << num1 << endl; cout << " +" << setw(4) << num2 << endl; cout << " " << "----" << endl; cout << " "; Ans(correctans); } void subtraction(double & correctans, double num1, double num2) { double studentans; //difficulty(num1, num2); //if ('h' : num2 > num1) //num2 = 1 + rand() % 999; correctans = num1 - num2; // Display the problem. cout << "\n\n"; cout << " " << setw(8) << num1 << endl; cout << " -" << setw(8) << num2 << endl; cout << " " << "----" << endl; cout << " "; Ans(correctans); } void multiplication(double & correctans, double num1, double num2) { double studentans; //difficulty(num1, num2); correctans = num1 * num2; cout << "\n\n"; cout << " " << setw(4) << num1 << endl; cout << " *" << setw(4) << num2 << endl; cout << " " << "----" << endl; cout << " "; Ans(correctans); } void division(double & correctans, double num1, double num2, char choice) { double studentans; num1 = num2 * num1; //difficulty(num1, num2); /*if (choice == 'b') { num2 = 1 + rand() % 9; num1 = num2 * (rand() % 50 + 1); } if (choice == 'm') { num2 = 1 + rand() % 20; num1 = num2 * (rand() % 100 + 1); } else if (choice == 'h') { num2 = 1 + rand() % 15; num1 = num2 * (rand() % 110 + 1); } */ // Calculate the correct answer. correctans= num1 / num2; cout << "\n\n"; cout << " " << num1 << " / " << num2 << " = "; Ans(correctans); }``````
 but the others elude me.
What do you mean? What is the problem? Please elaborate.
this is the part i cant do : i need it to keep track of the number of correct scores as well as number tries and i also want to have 5 answer options,and also give the user up to 3 tries to get a question right
Well, I'm not doing your homework. You need to be more specific what your problems are.
i'm not asking you to do my homework, i've done it as you can see. i'm having problems figuring out how to get it to loop the question up to 3 times if the user gets it wrong and also how to get it to display multiple choice options. I may be a beginner, but i'm not lazy or anything. i"m here because i'm stuck, you can run my code it works fine.

Here's an example of looping a question up to 3 times. Maybe it'll help:

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include int main() { using namespace std; std::string correct_answer = "dog"; std::string your_answer; int num_loops = 3; bool correct = false; for (int i = 0; i < num_loops; i++) { cout << "Question: What is a grown puppy called?" << endl; cout << "Answer: "; cin >> your_answer; if (your_answer == correct_answer) { correct = true; break; } else { cout << "Sorry, incorrect." << endl; } } if (correct) { cout << "Correct!" << endl; } else { cout << "You ran out of tries. =(" << endl; } }``````
Last edited on
thanks, if you look at my code, you'll see that i have a "void ans" function, that takes care of displaying the answers, is that where i'll put this loop? or will i have to make another function?
Last edited on
yes you could certainly put the loop logic inside of answer.

You could do something like
 ``123456789101112131415161718192021`` ``````void Ans(double correctans, char op int num1, int num2) { // ... for (int i = 0; i < num_loops; i++) { display_problem(op, num1, num2); cout << "Answer: "; cin >> your_answer; if (your_answer == correct_answer) { correct = true; break; } else { cout << "Sorry, incorrect." << endl; } } }``````

Also, you have some repeated code inside each +,-,*,/ function

 ``123456`` `````` // Display the problem. cout << "\n\n"; cout << " " << setw(4) << num1 << endl; cout << " +" << setw(4) << num2 << endl; cout << " " << "----" << endl; cout << " ";``````

 ``123456`` `````` // Display the problem. cout << "\n\n"; cout << " " << setw(8) << num1 << endl; cout << " -" << setw(8) << num2 << endl; cout << " " << "----" << endl; cout << " ";``````

 ``12345`` `````` cout << "\n\n"; cout << " " << setw(4) << num1 << endl; cout << " *" << setw(4) << num2 << endl; cout << " " << "----" << endl; cout << " ";``````

I would factor those into a function:

 ``12345678910111213141516`` ``````void display_problem(char op, int num1, int num2) { cout << "\n\n"; if (operator != '/') { cout << " " << setw(4) << num1 << endl; cout << " " << op << setw(4) << num2 << endl; cout << " " << "----" << endl; cout << " "; } else { cout << " " << num1 << " / " << num2 << " = "; } }``````

called like:
 ``12`` `````` display_problem('+'); // for addition display_problem('/'); // for division ``````
Last edited on