C++ Class calculator not working

So the assignment is to make a class calculator. The professor gave us a lot of help and very specific steps to follow so the code should work... But I think I didn't quite get what he was saying and the program is not working. Any help would be awesome!! This is what I got so far:

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
#include <iostream>
using namespace std;



    class Calculator {
	float final;



	public:

	Calculator( float in )
	{
		final = in;
	}


	void Calculator::add(float &result, float input)//Should I use this?
    {
	result += input;
    }

	void operator+( float in )// or should I use this?
	{
		final += in;
	}

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

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

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

	float GetTotal()
	{
		return final;
	}
};
	

int main()
{
	Calculator; //This is not working
	float input;
	char OP;

	cout << "################################################################################"<<endl;
	cout << "Greetings, you have initiated the class calculator program. You can add (+), subtract (-), " << endl;
	cout << "multiply (*), or divide (/). To work this calculator you enter a number " << endl;
	cout << "then press (Enter). Next enter the desiered operand, (+,-,*,/)." <<endl;
	cout << "continue like so until you have enter your last number. To get the final total, " << endl;
	cout << "press (=). Please note that order of operations are not considered." <<endl;
	cout << "To quit the program, enter (Q). You can now start" << endl;
	cout<<  "################################################################################"<<endl<<endl;

	cout << ">";
	cin >> input;

	Calculator calc( input ); 

	cout << ">";
	cin >> OP;

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

		switch((int)OP) 
		{
			case (int)'+':
				calc + input; //using class operators!!
				break;
			case (int)'-':
				calc - input;
				break;
			case (int)'*':
				calc * input;
				break;
			case (int)'/':
				if( input == 0 )
				{
					input = 1;
					cout << "You cannot devide by zero!!" << endl;
				}
				calc / input;
				break;
			default:
				cout << "Invalid!!!"<<endl;
				break;
		}

		cout << ">";
		cin >> OP;

	}while(OP != '=');

	cout << calc.GetTotal() << endl;

	system("PAUSE");
    return 0;
}
Last edited on
I didn't look at this in detail... but you are overloading your operators in a counter-intuitive way.

Example:
 
calc + input; //using class operators!! 


This code is modifying calc. But that's nonsense.

Consider the following:

1
2
3
int foo = 5;
int bar = 6;
int sum = foo + bar;  // you wouldn't expect this to change 'foo', would you? 


when you have x + y, you don't expect either x or y to change. Yet that's what's happening with your code.

Instead, you should change those operators to +=, -=, *=, etc, since those are expected to change the left operand.
ok you say I should change that to this: calc += input;?? I thought in that case calc was not being modified but just added. Anyways that calc part is killing me, on another part of the code for example I had something like this
1
2
3
4
5
int main()
{
	Calculator (calc); //This had an error and would not let the program start
	float input;
	char OP;


1
2
3
4
5
int main() //so I changed it to this, but now it starts but does not work.
{
	Calculator; 
	float input;
	char OP


In summary the whole code does not work... what is wrong with it?
Last edited on
ok you say I should change that to this: calc += input;??

Yes.

I thought in that case calc was not being modified but just added

+= is addition and assignment.

1
2
3
a += b;
// is the same as
a = a + b;


Calculator (calc); //This had an error and would not let the program start


The syntax is type variable.

Just like you say int myint;...

you would say Calculator mycalculator;

Here our type is "Calculator", and we're creating an instance of that calculator named "mycalculator" (although in your case I suppose you'd want to call it calc)
Last edited on
ok thanks! but the code still does not work:

I used Calculator calc; but the error says there is no default constructor. So I added the Calculator(); immediately after the public: but it still does not work.

I know there is stuff wrong in the header before the int main() but Im not sure what.

Also when I changed the wrong code to calc += input; it shows an error on the "+=" saying that no operator matches these operands
Last edited on
I used Calculator calc; but the error says there is no default constructor. So I added the Calculator(); immediately after the public: but it still does not work.


Give that constructor a body.

Also, in the future, please be more descriptive of the problem. Telling me you were receiving a "no default constructor" error is good. Saying "it doesn't work" is bad. That could mean any number of things.

I know there is stuff wrong in the header before the int main() but Im not sure what.


How do you know that?

Also when I changed the wrong code to calc += input; it shows an error on the "+=" saying that no operator matches these operands


That's because you're defining the + operator in your Calculator class. You need to change that to +=.

Do the same with -, * and / as well. (change to -=, *= and /=, both in the calculator class and in your switch statement.
Alright I think I did what you told me, I put the (+=...) on the calculator class and on the switch statements but the code still does not work. It says in the switch statements that using (+=...) is illegal on class.

About the header and class definition that was the part that I had the most trouble with and there was a lot of guessing thus I feel it might probably have an error. Finally I am not sure what type of body I should use in the constructor, should I use char? Here is the code so far:

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
#include <iostream>
using namespace std;



    class Calculator {
	float final;



	public:

		Calculator(); //needs body


	Calculator( float in )
	{
		final = in;
	}


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

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

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

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

	float GetTotal()
	{
		return final;
	}
};
	

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

	cout << "################################################################################"<<endl;
	cout << "Greetings, you have initiated the class calculator program. You can add (+), subtract (-), " << endl;
	cout << "multiply (*), or divide (/). To work this calculator you enter a number " << endl;
	cout << "then press (Enter). Next enter the desiered operand, (+,-,*,/)." <<endl;
	cout << "continue like so until you have enter your last number. To get the final total, " << endl;
	cout << "press (=). Please note that order of operations are not considered." <<endl;
	cout << "To quit the program, enter (Q). You can now start" << endl;
	cout<<  "################################################################################"<<endl<<endl;

	cout << ">";
	cin >> input;

	Calculator calc( input ); 

	cout << ">";
	cin >> OP;

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

		switch((int)OP) 
		{
			case (int)'+':
				calc += input; //error += illegal for class
				break;
			case (int)'-':
				calc -= input;
				break;
			case (int)'*':
				calc *= input;
				break;
			case (int)'/':
				if( input == 0 )
				{
					input = 1;
					cout << "You cannot devide by zero!!" << endl;
				}
				calc /= input;
				break;
			default:
				cout << "Invalid!!!"<<endl;
				break;
		}

		cout << ">";
		cin >> OP;

	}while(OP != '=');

	cout << calc.GetTotal() << endl;

	system("PAUSE");
    return 0;
}
Made it work never mind! Thanks also
How did you figure out how to program in order for the calculator to work? How did you know to use a for loop and while statement?
Topic archived. No new replies allowed.