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;
}

function calls look like:
add(input1, input2);

get first input
get operator 
if(secondOperandNeeded(operator))
  then get second operand
switch(operator)

function calls look like:
add(input1, input2);

get first input
get operator 
if(secondOperandNeeded(operator))
  then get second operand
switch(operator)

Thanks, that helped. Unfortunately I'm stuck again - for some reason the value for the input isn't being applied to the arithmetic functions. Can anyone help me spot the problem?

#include <iostream>
#include <cmath>
using namespace std;

int add(int op1, int op2);
int sub(int op1, int op2);
int mult(int op1, int op2);
int divs(int op1, int op2);
int mod(int op1, int op2);
int fact(int op1);
int exp(int op1, int op2);
char getOperator(char);
int getOp1(int op1);
int getOp2(int op2);


main ()
{
     cout << "Welcome to Calculator" << endl;
     int op1, op2;
     char opr;
     bool run = true;
     do
     {
          getOp1(op1);
          getOperator(opr);
          if (opr = '+','-','*','/','%','^') getOp2(op2);
          switch (opr) {
                     	case '+': cout << add(op1, op2) << endl; break;
                     	case '-': cout << sub(op1, op2) << endl; break;
                     	case '*': cout << mult(op1, op2) << endl; break;
                     	case '/': cout << divs(op1, op2) << endl; break;
                     	case '%': cout << mod(op1, op2) << endl; break;
                     	case '!': cout << fact(op1) << endl; break;
                     	case '^': cout << exp(op1, op2) << endl; break;
                     	case 'Q': run = false; break;
                     	default: cout << "That is not a valid operation." << endl;
                        }

     } while (run);
     system("pause");
     return 0;
}


char getOperator(char)
{
     char opr;
     cout << "Select an Operation (or select 'Q' to quit)\n";
     cout << "+, -, *, /, !, ^, Q \n";
     cin >> opr;
     return opr;
}

int getOp1(int op1)
{
    int i1;
    if (i1 < -1000 && i1 > 1000) {
            cout << "Please enter a different operand: ";
            cin >> i1;
            }
    cout << "Enter an operand: "; 
    cin >> i1;
    return i1;
}

int getOp2(int op2)
{
    int i2;
    if (i2 < -1000 && i2 > 1000) {
            cout << "Please enter a different operand: ";
            cin >> i2;
            }
    cout << "Enter another operand: "; 
    cin >> i2;
    return i2;
}
     
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) {
            cout << "Integer must be positive." << endl;
            }
    int ans=1;
    while ((op1 > 1) && (op1 < 20))
      ans *= op1--;
    return ans;
}

int exp(int op1, int op2)
{
    return op1^op2;
}

Edited 4 Years Ago by lw2025: n/a

if (opr = '+','-','*','/','%','^')

use the == operator for comparing values
Divide into different statements and Compare opr with each operand
and use || for all the statements comparing opr with a single operand to check if at least one of these statements is true

I am not certain, but I do not think that if (opr='+',...) works. I would suggest trying:

if (opr='+'||opr='-'||...)

I am not certain, but I do not think that if (opr='+',...) works. I would suggest trying:

if (opr='+'||opr='-'||...)

that's what == operator is for right ;)

Thank you guys :) Unfortunately, even if I add the getOp2() function to the switch the program still spits out some random garbage in reply :(

At the get input functions use the variables you passed on to those functions
assign values to the variables and return them
then at main, assign values to op1, op2 and opr using those functions like

op1 = getOp1(op1);

use cout statements after these to make sure you got the correct values

Edited 4 Years Ago by zeroliken: n/a

It's working now. Thank you all so much for your help!

This question has already been answered. Start a new discussion instead.