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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117`` `````` #include #include 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 : "<
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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141`` ``````#include 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 << '/' <

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116`` `````` #include #include 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 : "<
Last edited on
Registered users can post here. Sign in or register to post.