-
Examine my code please
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;
}
-
formatting is a wonderful thing.
-
You mean its good or do you mean its bad :-/
-
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;
}
?
-
I know I dont need all of them
I forgot to say im using this to learn functions
-
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.
-
-
>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;
}
-
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
-
>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.
-
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.
-
I would also switch those if statements to switch statements.
-
yah it was RoD
Thanks for the comment.
So I should use switch statements.
I usually dont use them unless wheres alot of choices
-
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: ";