I am trying to learn C++. I wrote a little calculator using class to see if I can do it and give some practice. The following is the code:
#include <iostream>
using namespace std;
class Calculator
{
float a, b;
public:
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);
};
float Calculator::add(float a, float b)
{
return (a+b);
}
float Calculator::subtract(float a, float b)
{
return (a-b);
}
int main()
{
Calculator calc;
float a,c;
char b;
cout << "Enter math problem (eg. 1 + 2)" << endl;
cin >> a >> b >> c;
switch (b)
{
case '+':
calc.add(a,b);
cin.ignore();
default:
cout << "Please Enter correct math problem" << endl;
}
}
When the command line pops up, it asks me to Enter math problem and I do, such as I do 1+2 and it returns the default switch "Please Enter correct math problem". Then I tried adding cout << "True"; to see if it was actually going to the correct switch and it printed true. So it seems that it isn't doing calc.add(a,b) though I am not sure why not.