I'm trying to pass pointers to a function so I can get 2 values.
I get garbage in the output
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 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#define PI 3.14159
using namespace std;
void arithmetic(int, int, float*, float*);
int main()
{
int inHeight; //height
int inRadius; //radius
cout > inHeight;
cout > inRadius;
float calcArea; //holds Area calculation
float calcVolumn; //holds Volume calculation
float *pArea = &calcArea; // stores address of area calculation
float *pVolumn = &calcVolumn; // stores address of volume calculation
arithmetic(inHeight,inRadius,pArea,pVolumn); // function call
cout << "Radius is: " << inRadius << ",\t" << "Height is: " << inHeight
<< "\n" << "*** using the variables ***\n" << "area: "
<< calcArea << "\t\n" << "volumn: " << calcVolumn << "\n"
<< "*** using the pointers ***\n" << "area: " << *pArea << "\t\n"
<< "volumn: " << *pVolumn;
}
//whats wrong with this
void arithmetic(int inHeight, int inRadius, float* pA, float* pV)
{
float cylinderVolumn = PI * (inRadius * inRadius) * inHeight; //calculates volume
float cylinderArea = (2.0 * PI * inRadius * inHeight) * (2.0 * PI *(inRadius * inRadius)); // calculates surface area
pV = &cylinderVolumn; //stores volume in pV
pA = &cylinderArea; //stores area in pV
}
|
1. Declare 2 ints named inHeight and inRadius
2. ask the user for 2 ints and store the values in inHeight and inRadius
3. declare the following 2 pointers
a. float * pArea
b. float * pVolumn
4. write a function with the following header
void arithmetic(int, int, float*, float*)
5. pass inHeight and inRadius and the 2 pointers to the function
6. in the function, calculate the volume and surface area of the cylinder and put the corresponding answer into the proper pointer address
7. output should look like the following and be printed from main(