Need help with Quit Function Please Help

When I select the options 0 should Quit the program ,but it asks me to enter another fraction without exiting . Can someone help me to fix this? thanks!

the Output should be : (sample)
3/4 + 2/5 = 23/20
2/3 * 3/5 = 6/15




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
 #include <iostream>
#include <string>

using namespace std;

// FUNCION OF EQUATIONS

void menu();
string addFractions(int num1, int den1, int num2, int den2);
string subtractFractions(int num1, int den1, int num2, int den2);
string multiplyFractions(int num1, int den1, int num2, int den2);
string divideFractions(int num1, int den1, int num2, int den2);

// MAIN FUNCTION
int main() {
menu();
return 0;
}

// MENU DRIVEN FUNCTION

void menu()
{
int choice=0, num1, num2, den1, den2, index;
string frac1, frac2, num;
do{
cout<<" Please Enter First Fraction Using format a/b: ";
cin>>frac1;
cout<<"Please Enter Second Fraction Using format a/b: ";
cin>>frac2;
cout<<"1. Add fractions\n2. Subtract fractions\n";
cout<<"3. Multiply fractions\n4. Divide fractions\n0. Exit\n";
cout<<"Please Enter Your Choice: ";
cin>>choice;


//USING STRING.SUBSTR
  
index=frac1.find("/");
num=frac1.substr(0,index);
num1=stoi(num);
num=frac1.substr(index+1);
den1=stoi(num);
index=frac2.find("/");
num=frac2.substr(0,index);
num2=stoi(num);
num=frac2.substr(index+1);
den2=stoi(num);

//SWITCH FUNCTION
  
switch(choice)
{
case 1:
cout<<"For Addition : "<<addFractions(num1,den1,num2,den2)<<"\n";
break;
case 2:
cout<<"For Substraction: "<<subtractFractions(num1,den1,num2,den2)<<"\n";
break;
case 3:
cout<<"For Multiplication: "<<multiplyFractions(num1,den1,num2,den2)<<"\n";
break;
case 4:
cout<<"For Division: "<<divideFractions(num1,den1,num2,den2)<<"\n";
break;
default:
choice=0;
}
}while(choice != 0);
{
    cout << "\nThe program has ended. Goodbye.\n";
}

}

string addFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = (num1*den2)+(num2*den1);
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string subtractFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = (num1*den2)-(num2*den1);
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string multiplyFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = num1*num2;
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string divideFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = num1*den2;
denominator = den1*num2;
result=to_string(numerator) + "/" + to_string(denominator);

return result;
}

Last edited on
Can someone help me identify where am I writing the code wrong?
There’s a number of ‘slips of the pen’ - I’m afraid the compiler won’t have any mercy of them.
Besides that, please note:
- filling the code with pointless comments makes it harder to read (I’ve deleted most of them just to understand what was going on);
- I’m pretty sure the maths in ‘divideFractions()’ is wrong;
- there’re still some bugs inside ‘outputResults()’ (the output is wrong);
- ‘outputResults()’ doesn’t perform any calculation on its arguments, so all of them could be passed by value;
- I haven’t tested the code: it compiles, but you’d better run it several times to ensure it’s correct;
- if you know 'switch', use it to improve your code readability;
- if you know 'struct', consider using them.
Here’s a compilable version of your code:
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
#include <iostream>

enum menuSelectionEnum {
    MENU_NONE       = 0,
    MENU_ADD        = 1,
    MENU_SUBTRACT   = 2,
    MENU_MULTIPLY   = 3,
    MENU_DIVIDE     = 4,
    MENU_QUIT       = 5,
    MENU_END        = 6
};

int menuItemSelection = 0;

int menu();
void inputFractionValuesFromUser(int &num1, int &num2, int &den1, int &den2);
void addFractions(int num1, int num2, int den1, int den2, int &resultNum1, int &resultDen);
void subtractFractions(int num1, int num2, int de1, int den2, int &resultNum, int &resultDen);
void multiplyFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen);
void divideFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen);
void outputResults(int num1, int num2, int den1, int den2, int &resultNum,
                    int &resultDen, int operation);

int main()
{
    int menuSelect = MENU_NONE;
    int num1 = 0, num2 = 0,den1 = 0, den2 = 0;
    int resultNum = 0, resultDen = 0;

    // loop until menu selection is "Quit" (Menu loop)
    do {
        menuSelect = menu();
        if (menuSelect == MENU_QUIT) { break; }
        inputFractionValuesFromUser(num1, num2, den1, den2);

        if (menuSelect == MENU_ADD) {
            addFractions( num1, num2, den1, den2, resultNum, resultDen);
        }

        if (menuSelect == MENU_SUBTRACT) {
            subtractFractions(num1, num2, den1, den2, resultNum, resultDen);
        }

        if (menuSelect == MENU_MULTIPLY) {
            multiplyFractions(num1, num2, den1, den2, resultNum, resultDen);
        }

        if (menuSelect == MENU_DIVIDE) {
            divideFractions(num1, num2, den1, den2, resultNum, resultDen);
        }
        outputResults(num1, num2, den1, den2, resultNum, resultDen, menuSelect);
    }
    while(menuSelect != MENU_QUIT);

    system("pause"); // http://www.cplusplus.com/forum/beginner/1988/
}

int menu ()
{
    int menuItemSelection = 0;
    do {
        std::cout << " Fraction Calculator Program\n"
                     " 1) Add Fractions\n"
                     " 2) Subtract Fractions\n"
                     " 3) Multiply Fractions\n"
                     " 4) Divide Fractions\n"
                     " 5) Quit\n"
                     " Please make a selection: ";
        std::cin >> menuItemSelection;

        if(menuItemSelection <= MENU_NONE || menuItemSelection >= MENU_END) {
            std::cout << " Invalid menu selection. Please Try again.\n";
        }
    } while (menuItemSelection <= MENU_NONE || menuItemSelection >= MENU_END);

    return menuItemSelection;
}

void inputFractionValuesFromUser(int &num1, int &num2, int &den1, int &den2)
{ 
    std::cout << "\nPlease enter the first numerator fraction: ";
    std::cin >> num1;

    std::cout << "\nPlease enter the denominator for the first fraction: ";
    std::cin >> den1;

    std::cout << "\nPlease enter the second numerator fraction: ";
    std::cin >> num2;

    std::cout << "\nPlease enter the second denominator fraction: ";
    std::cin >> den2;

    std::cout << '\n';
}

void addFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
    resultNum = (num1 * den2) + (num2 * den1);
    resultDen = den1 * den2;
}
void subtractFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
    resultNum = (num1 * den1) - (num2 * den2);
    resultDen =  den1 * den2;
}

void multiplyFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
    resultNum = num1 * num2;
    resultDen =  den1 * den2;
}

void divideFractions(int num1, int num2, int den1, int den2, int &resultNum, int &resultDen)
{
    resultNum = num1 * den2;
    resultDen =  den1 * num1;
}

void outputResults(int num1, int num2, int den1, int den2, int &resultNum, 
                    int &resultDen, int operation)
{
    if (operation == MENU_ADD) {
        std::cout << num1 << '/' << den1 << '+' << den2 << '=' 
                  << resultNum << '/' << resultDen << '\n';
    }

    if (operation == MENU_SUBTRACT) {
        std::cout << num1 << '/' << den1 << '-' << num2 << '/' << den2 << '='
                  << resultNum << '/' << resultDen << '\n';
    }

    if (operation == MENU_MULTIPLY) {
        std::cout << num1 << '/' << den1 << '*' << num2 << '/' << '='
                  << resultNum << '/' << resultDen << '\n';
    }

    if (operation == MENU_DIVIDE) {
        std::cout << num1 << '/' << den1 << '/' <<num2 << '/' << den2 << '='
                  << resultNum << '/' << resultDen << '\n';
    }
}

I don't know struct yet.Thank you for the help :) I"ve changed the code now

When I select the options 0 should Quit the program ,but it asks me to enter another fraction without exiting . Can someone help me to fix this? thanks!

the Output should be : (sample)
3/4 + 2/5 = 23/20
2/3 * 3/5 = 6/15

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

 #include <iostream>
#include <string>

using namespace std;

// FUNCION OF EQUATIONS

void menu();
string addFractions(int num1, int den1, int num2, int den2);
string subtractFractions(int num1, int den1, int num2, int den2);
string multiplyFractions(int num1, int den1, int num2, int den2);
string divideFractions(int num1, int den1, int num2, int den2);

// MAIN FUNCTION
int main() {
menu();
return 0;
}

// MENU DRIVEN FUNCTION

void menu()
{
int choice=0, num1, num2, den1, den2, index;
string frac1, frac2, num;
do{
cout<<" Please Enter First Fraction Using format a/b: ";
cin>>frac1;
cout<<"Please Enter Second Fraction Using format a/b: ";
cin>>frac2;
cout<<"1. Add fractions\n2. Subtract fractions\n";
cout<<"3. Multiply fractions\n4. Divide fractions\n0. Exit\n";
cout<<"Please Enter Your Choice: ";
cin>>choice;


//USING STRING.SUBSTR
  
index=frac1.find("/");
num=frac1.substr(0,index);
num1=stoi(num);
num=frac1.substr(index+1);
den1=stoi(num);
index=frac2.find("/");
num=frac2.substr(0,index);
num2=stoi(num);
num=frac2.substr(index+1);
den2=stoi(num);

//SWITCH FUNCTION
  
switch(choice)
{
case 1:
cout<<"For Addition : "<<addFractions(num1,den1,num2,den2)<<"\n";
break;
case 2:
cout<<"For Substraction: "<<subtractFractions(num1,den1,num2,den2)<<"\n";
break;
case 3:
cout<<"For Multiplication: "<<multiplyFractions(num1,den1,num2,den2)<<"\n";
break;
case 4:
cout<<"For Division: "<<divideFractions(num1,den1,num2,den2)<<"\n";
break;
default:
choice=0;
}
}while(choice != 0);
{
    cout << "\nThe program has ended. Goodbye.\n";
}

}

string addFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = (num1*den2)+(num2*den1);
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string subtractFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = (num1*den2)-(num2*den1);
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string multiplyFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = num1*num2;
denominator = den1*den2;
result=to_string(numerator) + "/" + to_string(denominator);
return result;
}

string divideFractions(int num1, int den1, int num2, int den2)
{
string result;
int numerator, denominator;
numerator = num1*den2;
denominator = den1*num2;
result=to_string(numerator) + "/" + to_string(denominator);

return result;
}
Last edited on
Topic archived. No new replies allowed.