Archived

This topic is now archived and is closed to further replies.

Problem with my simple C++ adding calculator.

This topic is 5382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having trouble with classes so to practice i am trying to make a simple adding calculator, I think the main problem (besides design hehe) is my return values on the member functions. I am having to put "return 0;" a the end of my member functions to avoid errors, if i try to leave that off and make the function return void i get tons of errors. Can someone please give me some tips on this program.
  #include <iostream>
using namespace std;

class Calculator
{
public:
	int GetInput();
	int Calculate(int x, int y);
	int RetAnswer();
private:
	int x, y;
	int Answer;
};

Calculator::GetInput()
{
	cout << "What is the first number: ";
	cin >> x;
	cout << "\nWhat is the second number: ";
	cin >> y;
	Calculator::Calculate( x,  y);
	cout << "The answer is " << Calculator::RetAnswer() << ".\n";
	return 0;
}

Calculator::Calculate(int x, int y)
{
	Answer = x + y;
	return 0;
}

Calculator::RetAnswer()
{
	return Answer;
}


int main()
{
	Calculator MyCalculator;
	MyCalculator.GetInput();
	return 0;
}  

Share this post


Link to post
Share on other sites
Are you really ommitting return types in your function defenitions, or is that just a typo? That's quite illegal, you know.

Also, I don't think you should use classes when it would be much simpler to go without them. Even if you're trying to learn how to use them, going out of your way to make convoluted class-based solutions to trivial problems isn't going to teach you anything.

[edited by - micepick on March 24, 2003 9:55:34 PM]

Share this post


Link to post
Share on other sites
Yeah, you haven''t even declared a return type, actually. You currently have: Calculator::Calculate(int x, int y), while it should be int Calculator::Calculate(int x, int y), since you DID declare it that way. If you declare it initially as void, I see no reason why you couldn''t have the functions return nothing.

On a sidenote, I liked the OO approach, but that''s just me

Share this post


Link to post
Share on other sites
ya I can read that crap all day but it really soaks in when i try to put it to use. DOH!, i just see the problem, god im stupid. I forgot that you have to declare a return type when calling classes functions. Thanks guys!

Share this post


Link to post
Share on other sites
quote:
Original post by Peon
On a sidenote, I liked the OO approach, but that''s just me


Maybe I''m just picky, but simply looking at that code makes me uncomfortable. You could get the same results with more elegance using one function and two variables.

If you really want to learn to use classes, find some interesting uses for inheritance and polymorphism. IMHO, using classes as fancy structs isn''t OOP.

Share this post


Link to post
Share on other sites