How can I use switch to use a particular class function from an object with values determined by the user?

Hello,

I have a task to make a simple fraction calculator using classes. I am having a particular issue where I cannot get my switch function to select a particular function in the object's class to perform the calculation.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <iostream>

using namespace std;

class fractions{

private:
    int A,B,C,D, SUM;

public:
    fractions(int = 1, int = 1, int = 1, int = 1, int = 0);

    void show_fractions();
    void sum_of_fractions(int, int, int, int, int);
    void diff_of_fractions(int, int, int,int);
    void product_of_fractions(int, int, int, int);
    void division_of_fractions (int, int, int, int);
};

fractions::fractions(int a, int b, int c, int d, int sum)
{
    A = a;
    B = b;
    C = c;
    D = d;
    SUM = sum;

    cout << " The constructor has created 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "  " << B << " " << C << " "<< D << endl;
}

void fractions::show_fractions()
{
    cout    << " The fractions are "
            << A << " / " << B << " and " << C << " / " << D << endl;
}

void fractions::sum_of_fractions(int a, int b, int c, int d, int sum)
{
    int denominator;

    A = a;
    B = b;
    C = c;
    D = d;

    SUM = sum;

    denominator = (B * D);

    bool denominator_ok = false;

    while (denominator_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " You got a denominator of" << e << endl;
            cout << " This isn't a valid denominator,please re-enter B"<< endl;
            cin >> B;
            cout << " D isn't non-zero, please put in a non-zero integer" << endl;
            cin >> D;

            continue;
        }
        denominator_ok = false;
    }

    sum = (((A * D) + (C * B)) / (B * D));

cout << " The sum of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
cout << A << "  " << B << " " << C << " "<< D << endl;
cout << " is " << sum;
}

void fractions::diff_of_fractions(int a, int b, int c, int d)
{

    int sum = 0;
    A = a;
    B = b;
    C = c;
    D = d;

    try
    {
        if (B == 0 || D == 0)
        {
            cout << " Re-enter a non-zero integer value for B" << endl;
                cin >> B;

            cout << " Re-enter a non-zero integer value for D" << endl;
                cin >> D;
        }
        else
        {
            sum = ((A * D) - (C * B)) / (B * D);

            cout << " The difference of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
            cout << A << "  " << B << " " << C << " "<< D << endl;
            cout << " is " << sum;
        }
    }
    catch (int e)
    {
        cout << " A denominator of " << e << " is invalid"<<endl;
    }
}

void fractions::product_of_fractions(int a, int b, int c, int d)
{
    int sum = 0;
    A = a;
    B = b;
    C = c;
    D = d;

    try
    {
        if (B == 0 || D == 0)
        {
            cout << " Re-enter a non-zero integer value for B" << endl;
                cin >> B;

            cout << " Re-enter a non-zero integer value for D" << endl;
                cin >> D;
        }
        else
        {
            sum = (A * C) / (B * D);

            cout << " The product of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
            cout << A << "  " << B << " " << C << " "<< D << endl;
            cout << " is " << sum;
        }
    }
    catch (int e)
    {
        cout << " A denominator of " << e << " is invalid"<<endl;
    }
}

void fractions::division_of_fractions(int a, int b, int c, int d)
{
    int sum = 0;
    A = a;
    B = b;
    C = c;
    D = d;

    try
    {
        if (B == 0 || C == 0)
        {
            cout << " Re-enter a non-zero integer value for B" << endl;
                cin >> B;

            cout << " Re-enter a non-zero integer value for D" << endl;
                cin >> D;
        }
        else
        {
            sum = (A * D) / (B * C);

            cout << " The product of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
            cout << A << "  " << B << " " << C << " "<< D << endl;
            cout << " is " << sum;
        }
    }
    catch (int e)
    {
        cout << " A denominator of " << e << " is invalid"<<endl;
    }
}

int main()
{
    fractions A;

    char option;
    int AA,BB,CC,DD;

    cout << " What would you like the fraction calculator to do ?" << endl;

    switch(option)
    {
        case'+':
            {
                A.sum_of_fractions(AA, BB, CC, DD, 0);
            }
            break;

        case'-':
            {
                A.diff_of_fractions(AA, BB, CC, DD);
            }
            break;

        case'*':
            {
                A.product_of_fractions(AA,BB,CC,DD);
            }
            break;

        case'/':
            {
                A.division_of_fractions(AA, BB, CC, DD);
            }
            break;

        default:
            cout<<"Wrong information entered. "<<"\nTry again. ";
    }

    cout << "Enter the fractions you'd like to add in the form a / b and c / d" << endl;

    cout << " The constructor has created 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << AA << "  " << BB << " " << CC << " "<< DD << endl;

    return 0;
}
Most likely you are getting an error of variables are not initialized because option hits the switch statement and option doesn't have a value and if it did AA,BB,CC,DD don't have values either. You need to initialize them or get user input for them.
Dear Joe,

Doh! Thanks for spotting that! I feel like a right dufus now for not putting in the cin >> command! I'll give that a go.
Hello Joe,

I managed to get it working and just simplified it to just use the different member functions of the class. Works nicely.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>

using namespace std;

class fractions{


private:
    int A,B,C,D, SUM;

public:
    fractions(int = 1, int = 1, int = 1, int = 1, int = 0);

    void show_fractions(float,float,float,float);
    float sum_of_fractions(float,float,float,float);
    float diff_of_fractions(float,float,float,float);
    float product_of_fractions(float,float,float,float);
    float division_of_fractions (float,float,float,float);
};

fractions::fractions(int a, int b, int c, int d, int sum)
{
    A = a;
    B = b;
    C = c;
    D = d;
    SUM = sum;

    cout << " The constructor has created 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "  " << B << " " << C << " "<< D << endl;
}

void fractions::show_fractions(float A,float B,float C, float D)
{
    cout    << " The fractions are "
            << A << " / " << B << " and " << C << " / " << D << endl;
}

float fractions::sum_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

        denominator = (B * D);

    cout << " The denominator of " << B << " x " << D << endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) + (C * B)) / (B * D));

cout << " The sum of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " and " << C << "/"<< D << endl;
cout << " is " << sum << endl;

return sum;

}

float fractions::diff_of_fractions(float A,float B,float C, float D)
{

    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * D);

    cout << " The denominator of (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) - (C * B)) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " - " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::product_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    denominator = (B * D);

    cout << " The denominator is (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * C) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " x " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::division_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * C);

    cout << " The denominator of (" << B << " x " << C << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * D) / (B * C));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " / " << C << "/"<< D << " = " << sum << endl;

return sum;
}

int main()
{
    float A,B,C,D;

    cout << "To calculate the sum, difference, product and division of 2 fractions in the form A / B and C / D you need to enter some numbers" << endl;

    cout << "What are the values of A?" << endl;
    cin >> A;

    cout << "What are the values of B?" << endl;
    cin >> B;

    cout << "What are the values of B?" << endl;
    cin >> C;

    cout << "What is the value of D?"<< endl;
    cin >> D;

    cout << "The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    fractions AA;

    //A.show_fractions(A,B,C,D);
    AA.sum_of_fractions(A,B,C,D);
    AA.diff_of_fractions(A,B,C,D);
    AA.product_of_fractions(A,B,C,D);
    AA.division_of_fractions(A,B,C,D);

    return 0;

    /*cout << " What would you like the fraction calculator to do ?" << endl;

    cin >> option;


    switch(option)
    {
        case'+':
            {
                A.sum_of_fractions(AA, BB, CC, DD, 0);
            }
            break;

        case'-':
            {
                A.diff_of_fractions(AA, BB, CC, DD);
            }
            break;

        case'*':
            {
                A.product_of_fractions(AA,BB,CC,DD);
            }
            break;

        case'/':
            {
                A.division_of_fractions(AA, BB, CC, DD);
            }
            break;

        default:
            cout<<"Wrong information entered. "<<"\nTry again. ";
    }
*/
}
Unfortunately, If I enter a zero value to test the try-catch loop, my program crashes. Not sure where I am going wrong.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>

using namespace std;

class fractions{


private:
    int A,B,C,D, SUM;

public:
    fractions(int = 1, int = 1, int = 1, int = 1, int = 0);

    void show_fractions(float,float,float,float);
    float sum_of_fractions(float,float,float,float);
    float diff_of_fractions(float,float,float,float);
    float product_of_fractions(float,float,float,float);
    float division_of_fractions (float,float,float,float);
};

fractions::fractions(int a, int b, int c, int d, int sum)
{
    A = a;
    B = b;
    C = c;
    D = d;
    SUM = sum;

    cout << " The constructor has created 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "  " << B << " " << C << " "<< D << endl;
}

void fractions::show_fractions(float A,float B,float C, float D)
{
    cout    << " The fractions are "
            << A << " / " << B << " and " << C << " / " << D << endl;
}

float fractions::sum_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

        denominator = (B * D);

    cout << " The denominator of " << B << " x " << D << endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) + (C * B)) / (B * D));

cout << " The sum of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " and " << C << "/"<< D << endl;
cout << " is " << sum << endl;

return sum;

}

float fractions::diff_of_fractions(float A,float B,float C, float D)
{

    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * D);

    cout << " The denominator of (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) - (C * B)) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " - " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::product_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    denominator = (B * D);

    cout << " The denominator is (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * C) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " x " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::division_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * C);

    cout << " The denominator of (" << B << " x " << C << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * D) / (B * C));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " / " << C << "/"<< D << " = " << sum << endl;

return sum;
}

int main()
{
    float A,B,C,D;

    cout << "To calculate the sum, difference, product and division of 2 fractions in the form A / B and C / D you need to enter some numbers" << endl;

    cout << "What are the values of A?" << endl;
    cin >> A;

    cout << "What are the values of B?" << endl;
    cin >> B;

    cout << "What are the values of B?" << endl;
    cin >> C;

    cout << "What is the value of D?"<< endl;
    cin >> D;

    cout << "The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    fractions AA;

    //A.show_fractions(A,B,C,D);
    AA.sum_of_fractions(A,B,C,D);
    AA.diff_of_fractions(A,B,C,D);
    AA.product_of_fractions(A,B,C,D);
    AA.division_of_fractions(A,B,C,D);

    return 0;
}
Shishykish wrote:
Unfortunately, If I enter a zero value to test the try-catch loop, my program crashes. Not sure where I am going wrong.

Why did you mark your question as solved then? If you did it because the code worked before, then mark as unsolved.
Hello,

I am sorry, I didn't notice that. I clicked on it twice by accident! I've since specified the problem in another post.

Topic archived. No new replies allowed.