Okay, the first project was to build a calculator that functioned as follows:
----------Using the example functions about, write a calculator program.
Allow for the user to enter a simple math functions to evaluate:
(Note: the '>' designates user input for cin)
>5
>+
>7
>=
result:12
or:
>5
>+
>7
>-
>2
>=
result: 10
Your program will need to evaluate as many numbers and operations until the user enters a '=', at which time the result will be printed
Your program will need a seperate function to evaluate each math operation ( +, -, *, / )
Your program will also need a function to determine which math function to call
The functions you have written for Project 1 have been setup to use read in ints.
You will need to *overload* these functions to also allow for floats----------
I researched all over and could not get int and float to overload. I submitted the following code for that project.
_CSC123_Project1
#include <cstdlib>
#include <iostream>
using namespace std;
void get_input(float& input1, float input2, float& total1, float total2);
float result,input2,total2,total1,input1;
char sign;
float add(float& total1, float input2){
return total2=total1+input2;
}
float sub(float& total1, float input2){
return total2=total1-input2;
}
float mult(float& total1, float input2){
return total2=total1*input2;
}
float div(float& total1, float input2){
return total2=total1/input2;
}
float total(float& total2){
return total2;
}
int main()
{
float first_num, second_num, result1, result2;
get_input(first_num, second_num, result1, result2);
return (0);
}
void get_input(float& input1, float input2, float& total1, float total2)
{
cout << "Hello, this is a running total calculator. You can add \"+\", subtract \"-\", " << endl;
cout << "multiply \"*\", and divide \"/\". To work this calculator you enter a number " << endl;
cout << "then press [Enter]. You then you enter the operand, \"+,-,*,/\" and [Enter]." <<endl;
cout << "continue this process until you have enter your last number. To get the total, " << endl;
cout << "press \"=\". The functions will work like this: 8+2-4*6/3=12 NOT 8+2-4*6/3=2." <<endl;
cout << "To quit the program, enter \"Q\". Have fun." << endl;
cout << ">";
cin >> total1;
do{
cin >> sign;
if (sign== '='){
result=total(total2);
cout << total2 << endl;
}else
cin >> input2;
if(sign=='+'){
total1=add(total1,input2);
total2=total1;
}else if(sign=='-'){
total1=sub(total1,input2);
total2=total1;
}else if(sign=='*'){
total1=mult(total1,input2);
total2=total1;
}else if(sign=='/'){
total1=div(total1,input2);
total2=total1;
}
}while((sign != 'q') || (sign != 'Q'));
system("pause");
}