addition w/ class inside the cpp file

i need help for this. i made this simple program for addition. this would suppose to get two integer (x and y) from main function and pass it to the class and get the sum of it but unfortunately when i enter two digit example 2 and 2 i get the output "the sum of 4301064 and 4734484 is -214321848". what is the problem with this program and what should be the correct code for this. tnks for anyhelp.

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

	class Arithmetic
 	{
	public:
	int addSum(int num1,int num2)
	{
	int sum;
	sum= num1 + num2;
	cout<<"the sum of "<<num1<<"and "<<num2<<"is "<<sum<<endl;
	return 0;
	}

	};
	int main()
	{
	Arithmetic mymath;
	int x,y;
	cout<<"enter 1st number: ";
	cin>>x;
	cout<<"enter 2nd number: ";
	cin>>y;
	mymath.addSum(x,y);
	
return 0;
}
Last edited on
I've just run this code without problem. I did have to comment out the #include <conio.h> line as it doesn't exist on my system. I suspect you don't need it. Also, I would reccommend #include <iostream> rather than #include <iostream.h> .

Other than that I can't see a problem.

EDIT: you need a space before 'and' and 'is' in your cout line :)
Last edited on
tnks... ^_^
Thats Ok, but I haven't fixed your original problem because I couldn't find one :)
Topic archived. No new replies allowed.