• 03-20-2004
TWIXMIX
I finished the new calculator

I figured out how to make it so the user could imput the signs instead of having to put 2 numbers seperated by a space.

Can you tell me waht you guys think?

Code:

```#include <iostream> using namespace std; double add (double a, double b)//Addition Function {     return (a + b); } double subtract (double c, double d)//Subtraction Function {     return (c - d); } double multiply (double e, double f)//Multiplication Function {     return (e * f); } double divide (double g, double h)//Division Function {     return (g / h); } int main () { double pick1, pick2; char choose, method;      cout << "*********************" << endl;     cout << "***The Calculator****" << endl;     cout << "**By : Ryan Nielson**" << endl;     cout << "*********************\n\n" << endl;     do     {         cout << "\n\nPlease type your question below. Use + to add, - to subtract," << endl;         cout << "* to multiply or / to divide.\n " << endl;         cin >> pick1 >> method >> pick2;                 if (method == '+')//addition                 cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;         else if (method == '-')//substraction                 cout << "\n" <<pick1 << method << pick2 << "=" << subtract (pick1, pick2) << endl;         else if (method == '*')//multiply                 cout << "\n" <<pick1 << method << pick2 << "=" << multiply (pick1, pick2) << endl;         else if (method == '/')//divide                 cout << "\n" <<pick1 << method << pick2 << "=" << divide (pick1, pick2) << endl;         else                 cout << "\nIncorrect input!!!!!" << endl;                 cout << "\nWould you like to [c]ontinue or [e]xit: ";         cin >> choose;     }while ((choose != 'e') && (choose != 'E')); return 0; }```
• 03-20-2004
RoD
formatting is a wonderful thing.
• 03-20-2004
TWIXMIX
You mean its good or do you mean its bad :-/
• 03-21-2004
Tronic
Well with your current method of distinction between which operation they want to put on the two numbers...umm....

You don't even need all of those functions......

Why don't you go.....

Code:

```if (operator == "*") {   cout << num1 * num2; }```
?
• 03-21-2004
TWIXMIX
I know I dont need all of them

I forgot to say im using this to learn functions
• 03-21-2004
Tronic
Oh, ok then :) In that case yes your code is quite adequite. But what I would do(personally), is put those if statements in there own function, to make main() smaller and your code easier to decipher and catch errors.
• 03-21-2004
TWIXMIX
how would I do that?
• 03-21-2004
Tronic
>how would I do that?
Code:

```void process() { double pick1, pick2; char choose, method;  do     {         cout << "\n\nPlease type your question below. Use + to add, - to subtract," << endl;         cout << "* to multiply or / to divide.\n " << endl;         cin >> pick1 >> method >> pick2;                 if (method == '+')//addition                 cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;         else if (method == '-')//substraction                 cout << "\n" <<pick1 << method << pick2 << "=" << subtract (pick1, pick2) << endl;         else if (method == '*')//multiply                 cout << "\n" <<pick1 << method << pick2 << "=" << multiply (pick1, pick2) << endl;         else if (method == '/')//divide                 cout << "\n" <<pick1 << method << pick2 << "=" << divide (pick1, pick2) << endl;         else                 cout << "\nIncorrect input!!!!!" << endl;                 cout << "\nWould you like to [c]ontinue or [e]xit: ";         cin >> choose;     }while ((choose != 'e') && (choose != 'E')); }```
Code:

```int main () {     cout << "*********************" << endl;     cout << "***The Calculator****" << endl;     cout << "**By : Ryan Nielson**" << endl;     cout << "*********************\n\n" << endl;     process(); return 0; }```
• 03-21-2004
TWIXMIX
after i posted that i just thought of that.
I also have to declar the pick and method variables in the processing function not in main() in order for it to work
• 03-21-2004
Tronic
>I also have to declar the pick and method variables in the processing function not in main() in order for it to work

Yes you would unless you declared an inline function.

*goes to edit his code* I had an inkling(is that the right word? lol) that there would be scope issues.
• 03-21-2004
RoD
what i meant is that your code seems very "busy" or "cluttered". I know its not exactly what you were asking us to examine, but its the first thing i notice.
• 03-21-2004
Tronic
I would also switch those if statements to switch statements.
• 03-21-2004
TWIXMIX
yah it was RoD
Thanks for the comment.
So I should use switch statements.
I usually dont use them unless wheres alot of choices
• 03-21-2004
WaltP
You also might consider making the code in the if's easier to read. Modify each from:
Code:

```if (method == '+')//addition                 cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;         else ...```
to
Code:

```if (method == '+')//addition     ans = add (pick1, pick2); else if (method == '-')//substraction     ans = subtract (pick1, pick2); else ... cout << "\n" <<pick1 << method << pick2 << "=" << ans << endl;         cout << "\nWould you like to [c]ontinue or [e]xit: ";```