Sunday, August 2, 2009

Sigh...c++ programming stumper...?

all right here is the deal, how do i fix this program? %26gt;%26gt;%26gt;





//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;


using namespace std;





void Getsum(int);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


square = pow(sum);





cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;





return 0;


}





void GetSum(int A, int B)


{


int S = A + B;


return;


}

Sigh...c++ programming stumper...?
You had some pretty simple errors...


1) You spelled the Getsum function prototype differently than you did in the function call


2) You are passing a different number of parameters from the function prototype than in the function


3)Where does int A, and Int B get a value from? If the program would have worked you would have added whatever was stored in memory from A and B, since you didn't initialize them.


4)Void functions DONT return anything





I came up with a few fixes listed below, the error that you are getting from the pow(sum); line is very easily fixed, simply use the correct preprocessor directive...cmath didn't seem to work for me.








//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;





using namespace std;





void GetSum(int num1, int num2);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


//square = pow(sum);





cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;





return 0;


}





void GetSum(int num1, int num2)


{


int S = num1 + num2;


return;


}


********************


Since I did not know the correct pre-processor directive, I did a minor re-write of your code, now function (now an int returning function) will return the value S.





//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;





using namespace std;





int GetSum(int num1, int num2);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


//square = pow(sum);


sum = GetSum(num1,num2);


cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;


system("pause");


return 0;


}





int GetSum(int num1, int num2)


{


int S = num1 + num2;


return S;


}
Reply:Glad I could help Report It

Reply:main changes to





...


cout %26lt;%26lt; "Their sum is " %26lt;%26lt; GetSum(num1,num2) %26lt;%26lt; endl;


...











void GetSum(int A, int B)


{


int S = A + B;


return;


}





changes to





int GetSum(int A, int B) {


return A + B;


}


No comments:

Post a Comment