Sorry, I originnaly posted this in the Geek Lounge:

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 just using float.

//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");
          }

The code may not be pretty but it works.
Now using the same program,we are supposed to do the following:

------So, for this project we're going to revisit our Project 1 Calculator.
Now, we're going to use our knowledge of classes and build a Calculator Class with the same functionality of our original calculator, but all of our functions are now going to be embedded in an object class.------

I am still having problems getting the overload to work with int& float. I don't know if it is how i set up the references or what. just setting up touse float, it does not i ger 1.#QNAN. If i set it up for int, it works butnodecimal.
here is the current code:

//FinalProject
#include <cstdlib>
#include <iostream>
using namespace std;

class Calculator {
public: 
        int add(int& ,int);
        float add(float&, float);
        int sub(int& ,int);
        float sub(float&, float);
        int mult(int& ,int);
        float mult(float&, float);
        int div(int& ,int);
        float div(float&, float);
};


//void get_input(int& result,int input1);
void get_input(float& result, float input1);
//int result,input1;
float result, input1;
char sign;

int Calculator::add(int& result,int input1)
{
    result+input1;
}

float Calculator::add(float& result, float input1)
{
      result+input1;
}

int Calculator::sub(int& result,int input1)
{
    result-input1;
}

float Calculator::sub(float& result, float input1)
{
      result-input1;
}
            
int Calculator::mult(int& result,int input1)
{
    result*input1;
}

float Calculator::mult(float& result, float input1)
{
      result*input1;
}

int Calculator::div(int& result,int input1)
{
    result/input1;
}

float Calculator::div(float& result, float input1)
{
      result/input1;
}               
                                              
int main()
{
    //int result1, first_num;
    float result1, first_num;
    get_input(result1, first_num);
    //Calculator c;
     
    return (0);
}

//void get_input(int& result,int input1)
void get_input(float& result, float input1)

{
     Calculator c;
     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 >> result;
     do{
         cout << ">";
         cin >> sign;
         if (sign== '='){
                    result;
                    cout << result << endl;
                    }else
                    cout << ">";
                    cin >> input1;
                    if(sign=='+'){
                                  result=c.add(result,input1);
                                  result=result;
                                  }else if(sign=='-'){
                                        result=c.sub(result,input1);
                                        result=result;
                                        }else if(sign=='*'){
                                              result=c.mult(result,input1);
                                              result=result;
                                              }else if(sign=='/'){
                                                    result=c.div(result,input1);
                                                    result;
                                                    }
                                                    }while((sign != 'q') || (sign != 'Q'));
          system("pause");
          }

Try to break the problem down into as small of parts as possible. Make a < 20 line program that demonstrates one problem you have having and post it for us. It will be much easier to teach you little bits at a time using this procedure than to simply write your code for you.

Lots of your code did nothing because it was incorrect. I would just use floats because if there are no decimals in the result (ie 5+5 = 10) it will output 10 not 10.0. I used class operators in this but if you want to keep with with pass by reference look at the bottom of this post.

//FinalProject
#include <iostream>
using namespace std;

class Calculator {
	float total;

	public:
	Calculator( float in )
	{
		total = in;
	}

	void operator+( float in )
	{
		total += in;
	}

	void operator-( float in )
	{
		total -= in;
	}

	void operator*( float in )
	{
		total *= in;
	}

	void operator/( float in )
	{
		total /= in;
	}

	float GetTotal()
	{
		return total;
	}
};

int main()
{
	Calculator calc;
	float input;
	char op;

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

	Calculator calc( input ); //sets the inital value of the calculator

	cout << ">";
	cin >> op;

	do
	{
		cout << ">";
		cin >> input;

		switch((int)op) //since switch statements only handle ints cast the char to an int
		{
			case (int)'+':
				calc + input; //using class operators to do operations
				break;
			case (int)'-':
				calc - input;
				break;
			case (int)'*':
				calc * input;
				break;
			case (int)'/':
				if( input == 0 )
				{
					input = 1;
					cout << "DIVIDE BY ZERO ERROR!" << endl;
				}
				calc / input;
				break;
			default:
				cout << "Invalid operator" << end; //late check
				break;
		}

		cout << ">";
		cin >> op;

	}while(op != '=');
	cout << calc.GetTotal() << endl;
	system("PAUSE");
    return 0;
}

This would be what you want for your add/subtract/multiply/divide functions

void Calculator::add(float &result, float input)
{
	result += input;
}
result = c.add(result, input1); //this is wrong even if you were to return a value because the add function already changes the value of result because you pass it by reference
c.add(result, input1); //correct

You can use if statements if you want but I think the switch statement is way cleaner
for handling all your operators.

Edit: Forgot to add that you need to put in a try catch because if you enter an int for a char or a char for an int then you will get an infinite loop of >'s displaying.

Edited 6 Years Ago by sfuo: n/a

This article has been dead for over six months. Start a new discussion instead.