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;
	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 '+':
		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.

You are sending the char '+' to your calc.add function.

Line 34 should be calc.add(a,c), but this is still not going to display anything, although it will do what you expect except you are not applying the return value to any local variable.

You also need to add a break statement. It is falling through into your default switch statement.

Still doesn't work. I even put in numbers to add in the code and same problem.

I had edited my post, try again. You are not going to display anything to the console unless you use cout.
I just tested the code with my changes to yours, and it works fine.

line 37: cout << calc.add(a,c);

Oh yeah, duh! Can't believe I forgot that....

Thanks for pointing out my blonde moment :D

No problem =p. Happy coding!

hi, line 14 float Calculator::add(float a, float b)
it's a constructor?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.