Question: C++ Instructions given to me to fix my code, but need help translating into working code. The whole code is a fraction calculator. I have

C++ Instructions given to me to fix my code, but need help translating into working code. The whole code is a fraction calculator. I have already made some changes.

1. The menu() function is missing a return. You have a return inside the while loop, but you never get to it since you either hit continue or break. It would be a lot neater if you just used getChar(string, string) from the kishio library. You could also #include and then do "c = toupper(getChar(prompt, allowed));" and that would take care of the error checking for the input and convert the input to uppercase. You would no longer need the cin or the if/else. In fact, you really wouldn't even need the variable c since you could do: "return toupper(getChar(prompt, allowed));" Of course, prompt and allowed have to be replaced with the appropriate strings. If you use kishio's getChar, then there is no need for a while loop since the validation loop is inside kishio::getChar(string, string).

2. You should also be using kishio::getInt to get the numerators and denominators. Since getInt accepts a prompt, you can condense your cout/cin statements for each of the numbers you get from the user into a call to getInt.

Below is the two parts of my code that have been partially changed

int main(int argc, char* argv[]) { int n1, d1, n2, d2, n3, d3; char c = menu(); if (c != 'Q' || c != 'q') { cout << "Enter the numerator of the first fraction: "; cin >> n1; cout << "Enter the denominator of the first fraction: "; cin >> d1; cout << "Enter the numerator of the second fraction: "; cin >> n2; cout << "Enter the denominator of the second fraction: "; cin >> d2; try { switch (c) { case 'A': case 'a': addFractions(n1, d1, n2, d2, n3, d3);

break;

case 'S': case 's': subtractFractions(n1, d1, n2, d2, n3, d3); break;

case 'M': case 'm': multiplyFractions(n1, d1, n2, d2, n3, d3); break;

case 'D': case 'd': if(n2 != 0) { divideFractions(n1, d1, n2, d2, n3, d3); } else { break; } } cout << "Result = " << n3 << " / " << d3 << endl;

} catch (invalid_argument e) { cout << e.what() << endl; } } return 0; }

----------------------------------------------------------------------------------------------------

char menu() { kishio::getChar(string, string); int c = toupper(getChar(prompt, allowed));

cout << "This program performs math operations on fractions. " << "You get to choose what type of operation and then " << "enter the numerator and denominator for the operation.$ << "Please choose one of the following: " << "A) Add fractions " << "S) Subtract fractions " << "M) Multiply fractions " << "D) Divide fractions " << "Q) Quit fractions "; cout << "Enter choice: "; return c; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!