please help me with this program

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?


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace 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
const int 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();
    return 0;
}
//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
    unsigned int number1 =0;
    unsigned int 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 dudnt enter ya";
    }
    }while(userchoice != 5);
} 
Topic archived. No new replies allowed.