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

### A Simple Function Help, Please!

This is the Question and following is my program. Could someone please help with it. I'm not getting anywhere. It keep giving the 2065 error code -- "cout is undeclared Identifier at the functions".

Modify Lab Five such that you use functions for each math calculation (Add, Subtract, Multiply, and Divide). Pass in parameters to each function for the values to use and the functions will return the result. Use a function to read in the numbers involved. These numbers will be doubles. Also write a function that reads in the operator and returns a boolean – true if the operator is valid, false if not valid. This function will have two parameters. First is a string of characters containing the valid operators. The second is a reference parameter where the operator will be placed if the operator entered is valid.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177`` ``````#include #include #include void getNum1(double); void getNum2(double); void getOperator(char &); void Add(char &); void Subtract(char &); void Result(char &); double Num1; double Num2; char OPR; bool test1, test2; bool test3; int main() { double Num1; double Num2; char OPR; getNum1(Num1); getOperator(OPR); getNum2(Num2); Result(OPR); } void getNum1(double Num1) { cout << "Enter the First Number: " << endl; cin >> Num1; if((Num1 >= 0) || (Num1 <= 0)) { test1 = true; } else { cout << "Not a Valid Number" << endl; goto: 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; goto: 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 '/': getNum2(Num2); 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 != '/')) { goto getNum1(Num1); } else { goto getNum2(Num2); } } void Add(char &OPR) { double Result; if((OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { Result = Num1 + Num2; } } void Subtract(char &OPR) { double Result; if((OPR != '+') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { Result = Num1 + Num2; } } void Result(char &OPR) { double Result; if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x')) { switch (Opp) { case '+': Result = Add(OPR); cout << "The Result is " << Add(OPR) << "\n" << endl; break; case '-': Result = Subtract(OPR); cout << "The Result is " << Subtract(OPR) << "\n" << 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; } Num1 = Result; } }``````

It keeps giving the error code 2065. Also I haven't plugged in the Multiplication function ad Divide Function. I just want to make sure first i get add and subtract, right. Please Help.
Last edited on
You need to specify
`using namespace std;`
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; } } } ``````

@clarkd

I personally hate dislike constructs like line 125. They are ugly & non-scalable, and best handled with a switch. They are especially not needed just prior a switch which does the same thing.

The code would benefit from a IsOperator function that returns a bool, rather repeating these tests throughout the code.

This is the psuedocode for a basic calculator:

 ``1234`` ``````//get number //get operator //get number //calc & print answer ``````

Looking at this, 185 LOC is too much, and it only does + and - so far. Remember to check for division by zero when you do that function. Be careful doing that with doubles - you really want to check whether the number is less than some arbitrary precision like 0.001 say.

You also have global variables, put them all in main() and send them to functions that need them as references.

You can also make use of the toupper function (which transforms a char to upper case) so you don't have to test variables twice.

Hope all goes well.
Topic archived. No new replies allowed.

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