### Help factoring a section of a program to a function

Hello all,

I was wondering if I could get some input on a project I turned in for class. The instructor knocked 2 points off of the assignment of a possible 15 total. He says that I should have factored my code that checks for a zero denominator into a function. With the way I output my messages based on the operand selected, I just don't see it? Any help would be greatly appreciated. Thank you.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203`` ``````#include #include #include using namespace std; void addFractions(int,int,int,int); void subtractFractions(int,int,int,int); void multiplyFractions(int,int,int,int); void divideFractions(int,int,int,int); void menu(); int main() { menu(); return 0; } void menu() { bool zero_found = false; // Zero based fractions are NOT allowed. bool valid = false; // Make sure user entered a valid operation. char operation = ' '; // Holds the value of our math operator. char solidus1, solidus2; // We need to throw out the / symbol when inputing data. int numerator1, denominator1, numerator2, denominator2; string instructions = "Data is entered in the following format: \nnumerator/denominator \n" ; cout << setw(50) << "Welcome to my fraction calculator program:" << endl << endl; cout << "Please select from the following functions:" << endl; cout << "'+' To add fractions press + then enter" << endl; cout << "'-' To subtract fractions press - then enter" << endl; cout << "'*' To multiply fractions press * then enter" << endl; cout << "'/' To divide fractions press / then enter" << endl; cout << endl << ">>"; do{ cin >> operation; if(operation == '+') { do{ cout << instructions; cout << "Enter first fraction to add :" << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to add:" << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) //tests for zero { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); zero_found = true; } else { zero_found = false; valid = true; addFractions(numerator1,denominator1,numerator2,denominator2); // perform calculations } } while(zero_found == true); } else if(operation == '-') { do{ cout << instructions; cout << "Enter first fraction to subtract:" << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to subtract:" << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); zero_found = true; } else { zero_found = false; valid = true; subtractFractions(numerator1,denominator1,numerator2,denominator2); } } while(zero_found == true); } else if(operation == '*') { do{ cout << instructions; cout << "Enter first fraction to multiply:" << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to multiply:" << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); zero_found = true; } else { zero_found = false; valid = true; multiplyFractions(numerator1,denominator1,numerator2,denominator2); } } while(zero_found == true); } else if(operation == '/') { do{ cout << instructions; cout << "Enter first fraction to divide:" << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to divide:" << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); zero_found = true; } else { zero_found = false; valid = true; divideFractions(numerator1,denominator1,numerator2,denominator2); } } while(zero_found == true); } else { cout << "Please select a valid operation!" << endl; } } while(!valid); } void addFractions(int numerator1, int denominator1, int numerator2, int denominator2) { int numeratorSum, denominatorSum; numeratorSum = numerator1 * denominator2 + numerator2 * denominator1; denominatorSum = denominator1 * denominator2; cout << "The value of " << numerator1 << "/" << denominator1 << " + " << numerator2 << "/" << denominator2 << " = " << numeratorSum << "/" << denominatorSum <
Well, a lot of the code in function menu() is very repetitive. At least I would take the parts which are common to all cases out and put them in a separate function.

What I see at first glance is the only difference between the various cases is a couple of words which are output, and which function is subsequently called. So you could use a string for the words, such as "add", "subtract" etc. and pass that as a parameter to the function. When it returns with the validated input, call the required function.

If you do that, function menu() will get shorter and easier to read. And making the code easy to read and follow is a big part of the task.

Now that may not be exactly what your instructor said, there is room for some difference of opinion here.
I agree with your prof on this one. The following sets of lines do eaxtly the same thing and should have been in a function.
51-57
78-84
104-111
129-135

 ``123456789`` ``````bool check_denominators (int denom1, int denom2) { if((denom1 == 0) || (denom2 == 0)) { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); return false; // denoms not good } return true; // denoms ok }``````

This is my suggested function:
 ``1234567891011121314151617181920212223242526272829303132`` ``````void getData(std::string op, int &num1, int & denom1, int &num2, int &denom2) { string instructions = "Data is entered in the following format: \n" "numerator/denominator \n" ; char solidus1, solidus2; // We need to throw out the / symbol when inputing data. bool zero_found = true; do { cout << instructions; cout << "Enter the first fraction to " << op << ":" << endl; cin >> num1 >> solidus1 >> denom1; cout << "Enter the second fraction to " << op << ":" << endl; cin >> num2 >> solidus2 >> denom2; if ((denom1 == 0) || (denom2 == 0)) { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); zero_found = true; } else { zero_found = false; break; } } while (zero_found == true); }``````
Thanks for the input everyone. This is what I came up with while pondering the question and leaving the forum for a bit. It still seems harsh to lose points on a perfectly running program.

Does anyone have advice on when to factor elements of a program into a function? I have a bad feeling I am doomed to repeat the same mistakes over and over (insanity defined!). I hope I'm not out of my league with my desire to become a programmer.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````bool getInputs(int& numerator1, char& solidus1, int& denominator1, int& numerator2, char& solidus2, int& denominator2, char& operation) { string strOperation = ""; switch(operation) { case '+': strOperation = "add :"; break; case '-': strOperation = "subtract :"; break; case '*': strOperation = "multiply :"; break; case '/': strOperation = "divide :"; break; default: cout << "Invalid operation selected!"; return false; } cout << "Data is entered in the following format: \nnumerator/denominator \n" ; cout << "Enter first fraction to " << strOperation << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to " << strOperation << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) //tests for zero { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); //zero_found = true; } }``````
That's ok. But I see no reason to have `solidus1` and `solidus2` as parameters, unless you are going to do something with that value. I assumed it was to be discarded.
I was just looking for a quick and dirty way to answer my own question in Visual Studio 2010, you are correct though, the solidus('s ?) is discarded and therefore not needed in the formal parameter. Also noted the function does NOT return a value as it should.

Now if I can only get my instructor to let me re-submit the assignment.

For those that care?? Here is the complete program.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201`` ``````#include #include #include using namespace std; void addFractions(int,int,int,int,int&,int&); void subtractFractions(int,int,int,int,int&,int&); void multiplyFractions(int,int,int,int,int&,int&); void divideFractions(int,int,int,int,int&,int&); void menu(); bool getInputs(int& numerator1, int& denominator1, int& numerator2, int& denominator2, char& operation) { string strOperation = ""; char solidus1, solidus2; switch(operation) { case '+': strOperation = "add :"; break; case '-': strOperation = "subtract :"; break; case '*': strOperation = "multiply :"; break; case '/': strOperation = "divide :"; break; default: cout << "Invalid operation selected!"; return false; } cout << "Data is entered in the following format: \nnumerator/denominator \n" ; cout << "Enter first fraction to " << strOperation << endl; cin >> numerator1 >> solidus1 >> denominator1; cout << "Enter the 2nd fraction to " << strOperation << endl; cin >> numerator2 >> solidus2 >> denominator2; if((denominator1 == 0) || (denominator2 == 0)) //tests for zero { cout << endl << "Error: denominator can not be zero!" << endl; cin.clear(); cin.ignore(std::numeric_limits::max(), '\n'); return true; } else { return false; } } int main() { menu(); return 0; } void menu() { bool zero_found = false; // Zero based fractions are NOT allowed. bool valid = false; // Make sure user entered a valid operation. char operation = ' '; // Holds the value of our math operator. int numerator1, denominator1, numerator2, denominator2; int numAnswer = 0; int denAnswer = 0; cout << setw(50) << "Welcome to my fraction calculator program:" << endl << endl; cout << "Please select from the following functions:" << endl; cout << "'+' To add fractions press + then enter" << endl; cout << "'-' To subtract fractions press - then enter" << endl; cout << "'*' To multiply fractions press * then enter" << endl; cout << "'/' To divide fractions press / then enter" << endl; cout << endl << ">>"; do{ cin >> operation; if(operation == '+') { do{ zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation); if(zero_found != true) { addFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations cout << "The value of these fractions added are: " << numAnswer << "/" << denAnswer << endl; valid = true; } } while(zero_found == true); } else if(operation == '-') { do{ zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation); if(zero_found != true) { subtractFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations cout << "The value of these fractions subtracted are: " << numAnswer << "/" << denAnswer << endl; valid = true; } } while(zero_found == true); } else if(operation == '*') { do{ zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation); if(zero_found != true) { multiplyFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations cout << "The value of these fractions multiplied are: " << numAnswer << "/" << denAnswer << endl; valid = true; } } while(zero_found == true); } else if(operation == '/') { do{ zero_found = getInputs(numerator1,denominator1,numerator2,denominator2,operation); if(zero_found != true) { divideFractions(numerator1,denominator1,numerator2,denominator2, numAnswer, denAnswer); // perform calculations cout << "The value of these fractions divided are: " << numAnswer << "/" << denAnswer << endl; valid = true; } } while(zero_found == true); } else { cout << "Please select a valid operation!" << endl; } } while(!valid); } void addFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum) { numSum = numerator1 * denominator2 + numerator2 * denominator1; denSum = denominator1 * denominator2; } void subtractFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum) { numSum = numerator1 * denominator2 - numerator2 * denominator1; denSum = denominator1 * denominator2; } void multiplyFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum) { numSum = numerator1 * numerator2; denSum = denominator1 * denominator2; } void divideFractions(int numerator1, int denominator1, int numerator2, int denominator2, int& numSum, int& denSum) { numSum = numerator1 * denominator2; denSum = denominator1 * numerator2; } ``````
Last edited on
Topic archived. No new replies allowed.