Assignment problem need help

Hello all,
Can any one help me find and correct the errors in the below c++ program please....

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
 #include <iostream>
#include <conio.h>
class testclass
{
	private:
	int n1,n2,sum;
	public:
		void setvalue(int d1, int d2)
		{n1=d1; n2=d2;}
        void calc()
		{sum=n1+n2;}
		void disresult()
	}
		{
			cout<<"sum of value is "<<sum;
				}
void main()		
{
	clrscr();
	testclass obj1, obj2;
	obj1.setvalue(20,30);
	obj2.setvalue(50,40);
	obj1.disresult();
	obj2.disresult();
	getch();
}
}
Hello junaidkkc,

In the end this is a good example of what not to do.

First I would use a good IDE to put the code in. This should help to make thing look better and help you spot the errors. Two suggestions I have are Visual Studio, that I use, and Code::Blocks. Either will do a good job of indenting your code properly. Providing the code is written properly.

Try compiling the program and see what error messages you get. This is always a good place to start.

A hint void main(). This is no longer used. If your compiler does not find this to be an error then I would say that your IDE/compiler is old and needs updated.

Two more hints:

"conio.h" is a header file that is not available to everyone these days, so there are some people that would not be able to use your code. The same holds true for "clrscr()".

There are several things that need fixed and several part missing, mostly with the class.

Hope that helps,

Andy
//Try this code. I have removed errors.



#include<iostream>
#include<conio.h>
using namespace std;
class testclass
{
int n1,n2,sum;
public:
void setvalue(int d1, int d2)
{
n1=d1;
n2=d2;
calc();
}
void calc()
{
sum=n1+n2;
}
void disresult()
{
cout<<sum<<endl;
}
};
int main()
{
testclass obj1, obj2;
obj1.setvalue(20,30);
obj2.setvalue(50,40);
obj1.disresult();
obj2.disresult();
}
Last edited on
Thank you Andy and ammadahmed ♥
Topic archived. No new replies allowed.