This is the prescribed question.
Write a program that will repeatedly ask the user to enter the dimensions of a can and then calculate and display the total surface area and volume of a can. The program should prompt for and read the can's top radius and height in main and then call a function, calc. This function will receive via arguments the can's top radius and height and will perform the required surface area and volume calculations. The surface area and volume values should be “returned” to main via reference parameters. Then, main should display the can’s surface area and volume, and then ask the user if
she wants to enter information for another can. Note: Use a global constant for the value of Pi =3.141592654.
Note: The function calc should not contain any cout or cin statements.
Formulas: Volume = V = Pi r^2h, surface area = A = 2 Pi rh + 2 Pi r^02; h = can height and r = top radius
Here is an example of what output should look like from running your program (user input is shown
in bold):
Enter can radius (inches): 3.5
Enter can height (inches): 6.5
Can surface area 219.911 square inches
Can volume = 250.149 cubic inches
Enter another (y or n)? y
Enter can radius (inches): 10.1
Enter can height (inches): 22.9
Can surface area 2094.18 square inches
Can volume = 7338.85 cubic inches
Enter another (y or n)? n
I have everything I think but my numbers aren't calculating is it because I am using a void function?
Also do I need to change some of my ints to double or double&??
So This is what I have soo far:
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
|
#include <iostream>
#include <string>
using namespace std;
void calc(int surface_area,int volume,int r, int h);
void output_calc(int surface_area, int volume, int r, int h);
const double PI = 3.141592654;
int main ()
{
int surface_area, volume, h, r;
char ans;
do {
cout<<"Enter can radius (inches): ";
cin>> r;
cout<<"Enter can height (inches): ";
cin>>h;
calc (surface_area, volume, r, h);
output_calc(surface_area, volume, r, h);
cout<<"Enter another (y or n)?";
cin>>ans;
}
while (ans !='n' && ans != 'N');
return 0;
}
void calc(int surface_area,int volume, int r, int h)
{
const double PI = 3.141592654;
volume = PI * r * r * h;
surface_area = (2* PI * r * h) + (2 * PI * r * r);
}
void output_calc(int surface_area, int volume, int r, int h)
{
cout<<"Can surface area "<< surface_area << " Square inches"<< endl;
cout<<"Can volume = "<< volume<< " cubic inches"<< endl;
}
|
When it outputs the final answers surface area is always 0 and volume is 1..