Monday, May 24, 2010

Simple addition program in C++?

here is my code





#include %26lt;iostream.h%26gt;


#include %26lt;math.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;





class calculator


{


public:


calculator() {}


void input();


void addition();


private:


};


void input() // This function will get the two integers


{


int num1 = 0;


int num2 = 0;


cout %26lt;%26lt; "Enter the 1st number: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "Enter the 2nd number: ";


cin %26gt;%26gt; num2;


}





void addition() // This function will perform the operation


{


int num1 = 0;


int num2 = 0;


int result = 0;


result = num1 + num2;


cout %26lt;%26lt; "The result is " %26lt;%26lt; result;


}





void main()


{


calculator c;


c.input();


c.addition();


}





I'm getting errors, can someone assist me

Simple addition program in C++?
I'm not totally clear on what you're trying to do, but I have corrected your code a bit:








#include %26lt;iostream.h%26gt;


#include %26lt;math.h%26gt;


#include %26lt;ctype.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;





class calculator


{


public:


calculator() {}


void input();


void addition();


private:


int num1,num2;


};


void calculator::input() // This function will get the two integers


{


cout %26lt;%26lt; "Enter the 1st number: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "Enter the 2nd number: ";


cin %26gt;%26gt; num2;


}





void calculator::addition() // This function will perform the operation


{


int result = 0;


result = num1 + num2;


cout %26lt;%26lt; "The result is " %26lt;%26lt; result;


}





void main()


{


calculator c;


c.input();


c.addition();


}
Reply:First and foremost, there is a HUGE LOGICAL ERROR. Even if you get the program to work, it will still give you always zero because you are resetting the num1 and num2 variables RIGHT before you add them. What happens to the value you input??? They become zero and then you add them and display the answer which will always be zero.





Second, why do your libraries at the top end with .h?





Why are you writing the program like this? Why do you have all these subroutines? Are you supposed to practicing in creating subroutines and then passing values from one subroutine to another? You need to declare the variables as global variables so that their values can pass from one function to another. There are a lot of other things that don't make sense. I can't even write them all here.





First try doing it all only in the main function. And why is your main voided. Make it return zero at the end. The return zero is there for a reason.
Reply:You need to declare num1 and num2 class level variables for them to keep their values across method calls. Also, Cin object only accepts a character string. You then cast the string as a number.





Include the proper headers





class calculator


{


private:


int num1;


int num2;


public:


calculator() {}


void input();


void addition();


private:


};


void input() // This function will get the two integers


{


string tmp1;


string tmp2;


cout %26lt;%26lt; "Enter the 1st number: ";


cin %26gt;%26gt; tmp1;


cout %26lt;%26lt; "Enter the 2nd number: ";


cin %26gt;%26gt; tmp2;





num1=(int)tmp1;


num2=(int)tmp2;


}





void addition() // This function will perform the operation


{


cout %26lt;%26lt; "The result is " %26lt;%26lt; num1+num2;


}





void main()


{


calculator c;


c.input();


c.addition();


}











Can you also include the output of the debugger
Reply:I think you have a logic error here - you don't need to - in fact should not - assign the zero values to the 'num1' and 'num2' variables in the 'addition' method; the answer will always be 0. You need to remove the first 2 lines of code in the addition method. Also, you need to include properties for the num1 and num2 variables if you want to use them throughout the program. Either that or declare them as global variables. The way you have it now, they are in local scope in the input and addition methods, so once you take them out of the addition method, they will be out of scope when you call it in the main method.
Reply:I don't know anything about c++, but your variables num1 and num2 are local the way you have them in input() and addition().


You should probably have them as private members of the calculator class.


No comments:

Post a Comment