I'm trying to write a simple calculator that uses functions, but I'm really struggling to understand. Technically I'm supposed to have defined functions to enter both operands, but I'm confused about passing the data once it's been entered. I also need a function that decides if the second operand is necessary, but I can't figure out how to go about that. I've googled and searched here, but I can't seem to figure out how to apply what I find to my program.
Here's what I've got.
#include <iostream>
#include <cmath>
using namespace std;
int add(int, int);
int sub(int, int);
int mult(int, int);
int divs(int, int);
int mod(int, int);
int fact(int);
int exp(int, int);
int log10(int);
char getOperator();
main ()
{
cout << "Welcome to Calculator" << endl;
int input1, input2;
cout << "Enter the first Operand: ";
cin >> input1;
bool run = true;
do
{
switch ( getOperator() ) {
case '+': cout << "Enter the second Operand: ";
cin >> input2;
int add(int input1, int input2);
break;
case '-': cout << "Enter the second Operand: ";
cin >> input2;
int sub(int input1, int input2);
break;
case '*': cout << "Enter the second Operand: ";
cin >> input2;
int mult(int input1, int input2);
break;
case '/': cout << "Enter the second Operand: ";
cin >> input2;
int divs(int input1, int input2);
break;
case '%': cout << "Enter the second Operand: ";
cin >> input2;
int mod(int input1, int input2);
break;
case '!': int fact(int input1); break;
case '^': cout << "Enter the second Operand: ";
cin >> input2;
int exp(int input1, int input2);
break;
case 'L': int log10(int input1); break;
case 'Q': run = false; break;
default: cout << "That is not a valid operation." << endl;
}
} while (run);
system("pause");
return 0;
}
char getOperator()
{
char opr;
cout << "Select an Operation (or select 'Q' to quit)\n";
cout << "+, -, *, /, !, ^, L \n";
cin >> opr;
return toupper(opr);
}
int add(int op1, int op2)
{
return op1+op2;
}
int sub(int op1, int op2)
{
return op1-op2;
}
int mult(int op1, int op2)
{
return op1*op2;
}
int divs(int op1, int op2)
{
return op1/op2;
}
int mod(int op1, int op2)
{
return op1%op2;
}
int fact(int op1)
{
if (op1 < 0) return 0;
int ans=1;
while (op1 > 1)
ans *= op1--;
return ans;
}
int exp(int op1, int op2)
{
return op1^op2;
}