You are using a version without Ads of this website. Please, consider donating:

### Need Help Correcting the Function!!!!!!!

I'm suppose to write a calculator program with functions. The Program runs ask for the Operator and the second number but for some minor mistake it doesn't add or subtract. and ask for second number twice before giving the result. And when Press 'C' it clears and doesn't ask for operator.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186`` ``````#include #include #include using namespace std; void getNum1(double); void getNum2(double); void getOperator(char &); void Add(char &); void Subtract(char &); void Result(char); double Num1, Num2; char OPR; bool test1, test2; bool test3; double Result1; double Result2; int main() { getNum1(Num1); do { getOperator(OPR); getNum2(Num2); Result(OPR); } while(OPR); } void getNum1(double One) { One = Num1; cout << "Enter the First Number: "; cin >> Num1; if((Num1 >= 0) || (Num1 <= 0)) { test1 = true; } else { cout << "Not a Valid Number" << endl; getNum1(Num1); } } void getNum2(double Num2) { cout << "Enter the Second/Next number: "; cin >> Num2; if ((Num2 >= 0) || (Num2 <= 0)) { test2 = true; } else { cout << "Not a Valid Number" << endl; getNum2(Num2); } } void getOperator(char &OPR) { cout << "Enter the Operator: "; cin >> OPR; if((OPR != 'C' && OPR != 'c') || (OPR != 'X' && OPR != 'x')) { switch(OPR) { case '+': case '-': case '*': case '/': test3 = true; break; case 'C': case 'c': cout << "The Program is Cleared " << endl; test3 = true; break; case 'X': case 'x': cout << "\t--------TERMINATED---------\t" << endl; exit(0); default: if((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { cout << "Must Be An Operator(+-*/)" << endl; cout << "Enter an Operator: "; cin >> OPR; } } } if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/')) { getNum1(Num1); } else if((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { getNum2(Num2); } } void Add(char &OPR) { if((OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { Result1 = Num1 + Num2; } } void Subtract(char &OPR) { if((OPR != '+') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { Result2 = Num1 + Num2; } } void Result(char OPR) { if ((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { switch (OPR) { case '+': Add(OPR); cout << "The Result is " << Result1 << endl; break; case '-': Subtract(OPR); cout << "The Result is " << Result2 << endl; break; //case '*': // Result= Num1 * Num2; // cout << "The Result is " << Result << "\n" << endl; // break; //case '/': // Result = Num1 / Num2; // if (Num2 <= 0 && Num2 >= 0) // { // cout << "Error, Cannot Divide By Zero, The Result will always be Nothing == " << Result << endl; // exit(0); // } // else // cout << "The Result is " << Result << "\n" << endl; // break; case 'C': case 'c': cout << "The Program Cleared " << endl; test3 = true; break; case 'X': case 'x': cout << "You're Terminated :P/n" << endl; exit (0); default: cout << "**Must Be A Number**" << endl; } if(OPR = '+') { Num1 = Result1; } else { Num1 = Result2; } } } ``````

Last edited on
Hi, for your function, `getNum1(double One)`, `getNum2(double Num2)`..., you have to add '&' before the One and Num2...

 http://www.cplusplus.com/forum/beginner/96539/

@clarkd

I gave you a whole lot advice in the other thread, - if you have more questions, continue the original thread don't start a new one. Especially when you haven't taken on board all the advice given.

It's annoying from osgwsy's point of view because he spent some time to give some advice, only to find lots of the same advice has been given elsewhere on the same topic.

Edit: changed some wording

Last edited on
To add on, I go through your code, I find some of the lines are a bit weird.

For example, Line 42 and Line 58, `if((Num1 >= 0) || (Num1 <= 0)) `. I find this condition a bit redundant? What do you mean by the number is either less or equal zero or more than or equal zero?

In addition, Line 74, you added a if else condition before the switch. For the if else condition, you state no 'c'/'C' or 'x'/'X', however, you add two cases in the switch for 'c'/'C' and 'x'/'X', which does not make sense to me. You might want to check this part too.

About asking the second number twice, the reason is due to Line 111. In Line 111, it ask for getNum2(), then Line 30 ask for the second number again.

I think you mean want to draft what are the functions you might need, and organize your codes?
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: