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:
#include <iostream>
usingnamespace std;
class Calculator {
float final;
public:
Calculator( float in )
{
final = in;
}
void Calculator::add(float &result, float input)//Should I use this?
{
result += input;
}
voidoperator+( float in )// or should I use this?
{
final += in;
}
voidoperator-( float in )
{
final -= in;
}
voidoperator*( float in )
{
final *= in;
}
voidoperator/( 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;
}
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?
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)
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
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:
#include <iostream>
usingnamespace std;
class Calculator {
float final;
public:
Calculator(); //needs body
Calculator( float in )
{
final = in;
}
voidoperator+=( float in )
{
final += in;
}
voidoperator-=( float in )
{
final -= in;
}
voidoperator*=( float in )
{
final *= in;
}
voidoperator/=( 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;
}