cant get overloading of functions to work

we had to do a project as described:

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 overloading but just couldn't figure it out so I completed the project,but only used float.

Now we have to use the same project but setit upas a class. I still have not figureed out how to overload.

The two problems I am having are:

1. If I just use int, it works just no decimal. If I use float I get 1.#QNAN.
2. can I get help with overloading.

here is the code for the current project. it is setup for float.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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=result+input1;
}

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

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

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

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

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

float Calculator::div(float& result, float input1)
{
      result=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=result;
                                                    }
                                                    }while((sign != 'q') || (sign != 'Q'));
          system("pause");
          }
Topic archived. No new replies allowed.