Here is an example of a basic C++ Calculator with function calls. It was an assignment for a Class.
#include <cstdlib>
#include <iostream>
using namespace std;
void subtract(float[],int);
void multiply(float[],int);
void add(float[],int);
void divide(float[],int);
void CalculatorWithFunctionCalls(int);
int main()
{
int length;
string name, feel;
cout<<"Hello, what is your first name? ";
getline(cin,name);
cout<< "How are you, " << name<<" ? ";
cin>> feel;
if (feel == ("good")|| feel== ("Good") || feel==("alright")||
feel==("ok")|| feel== ("Okay"))
cout<< "That is good! Lets get started!"<<endl;
else
cout<<"Well I hope your day gets better!"<<endl;
cout<<"How many numbers would you like to input?";
cin>> length;
CalculatorWithFunctionCalls(length);
system("PAUSE");
return 0;
}
void CalculatorWithFunctionCalls(int length)
{
int counter ;
string answer;
float number[length];
char symbol;
bool again=false;
do
{
for( counter= 0; counter <length;counter ++)
{
cout<<"Enter number "<<counter+1<<": ";
cin>> number[counter];
}
cout<<"Enter a math symbol you would like(+,-,/,*): ";
cin>> symbol;
switch(symbol)
{
case '-':
subtract(number,length);
break;
case '*':
multiply(number,length);
break;
case '+':
add(number,length);
break;
case '/':
divide(number,length);
break;
}
cout<<"Would you like to do another calculation?(Y or N) ";
cin>> answer;
if (answer == ("Y") || answer == ("y"))
again=true;
else if (answer == ("N") || ("n"))
again=false;
else
cout<< "Invalid response";
}
while (again);
cout<<"Have a good day!\n";
}
void subtract(float num[] , int length)
{
int counter;
float temp=0;
for (counter = 0; counter < length; counter++)
{
temp= (num[counter] - temp);
}
cout<<"Your subtraction equation equals: " << temp<<endl;
}
void multiply(float num[] , int length)
{
int counter;
float temp=1;
for (counter = 0; counter < length; counter++)
{
temp= (num[counter]* temp);
}
cout<<"Your multiplication equation equals: " << temp<<endl;
}
void add(float num[] , int length)
{
int counter;
float temp;
for (counter = 0; counter < length; counter++)
{
temp= (temp+ num[counter]);
}
cout<<"Your addition equation equals: " << temp<<endl;
}
void divide(float num[] , int length)
{
int counter;
float temp=1;
for (counter = 0; counter < length; counter++)
{
temp= (num[counter]/ temp);
}
cout<<"Your division equation equals: " << temp<<endl;
}