My calculations aren't calculating

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..
Last edited on
you have to pass by reference if you want a changed input parameter to stick when the function executes.

void calc(int &surface_area,int &volume, int r, int h)
Last edited on
@dumdumFlame

Problems.. Variables surface_area, volume, r and h should be double, not int. You are not entering only whole numbers. And yes, one of your problems IS the void function, calc. Void does not return values. Use double calc(...).
1
2
3
4
5
6
7
8
9
10

surface_area = calc (r, h);
double calc( double r, double h ) // All that's needed for the function. Change declaration
{
	const double PI = 3.141592654; // Not needed. Already declared before main()
	// volume = PI * r * r * h; // Put this in the output_calc function. Not used or
       //  needed here
	double surface_area = (2* PI * r * h) + (2 * PI * r * r);
return surface_area;
}


There may be other small problems, but you should be able to iron those out after wards.
Last edited on
well It did work when I added the &. The only problem was that it didn't calculate decimal points.
Ok so I replaced all of the ints into doubles and void into double but then I would input things and nothing would output.
Here is the program that only works with whole numbers
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
#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 & whole number): ";
	cin>> r;
	cout<<"Enter can height (inches & whole number): ";
	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);
	surface_area = (2 * PI * r * r) + surface_area;

}
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;
	
}


Here is what I have so far on if it had decimal points. My current problem is the numbers outputted are not in the format I want and they constantly give out 4.94066e-324 for surface area and 2.61855e-322 for volume.

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
42
43
44
45
46
47
#include <iostream>
#include <string>


using namespace std;
double scalc(double &surface_area,double r, double h);
double vcalc(double& volume, double r, double h);
void output_calc(double &surface_area, double &volume, double r, double h);
const double PI = 3.141592654;
int main ()
{
	double surface_area, volume, h, r;
	char ans;
do {
	cout<<"Enter can radius (inches & whole number): ";
	cin>> r;
	cout<<"Enter can height (inches & whole number): ";
	cin>>h;
	double scalc (double& surface_area,double r,double h);
	double vcalc (double& volume,double r,double h);
	output_calc( surface_area, volume, r, h);
	cout<<"Enter another (y or n)?";
	cin>>ans;
}
while (ans !='n' && ans != 'N');//Ik it says if not N then return 0 but it only works this way.
return 0;
}
double scalc(double &surface_area, double r, double h)
{
	const double PI = 3.141592654;
	
	surface_area = (2* PI * r * h) + (2 * PI * r * r);
	return surface_area;
}
double vcalc(double& volume, double r, double h)
{
	const double PI = 3.141592654;
	volume = PI * r * r * h;
	return volume;
}
void output_calc(double &surface_area, double &volume, double r, double h)
{	

	cout<<"Can surface area "<< surface_area << " Square inches"<< endl;
	cout<<"Can volume = "<< volume<< " cubic inches"<< endl;
	
}



The second one still works with whole numbers though.
Last edited on
Also are you sure I shouldn't be using a float for some of the things like maybe r and h?
Also do I need to add a set precision function so when it does finally work it will only show the first significant figure? (I would add iomanip and the setiosflag stuff to make it it defined)
Last edited on
also @whitenite1
Here is a link of something where void does return the things it needs.
http://cpp.sh/7iztk5
This program inputs the radius then calculates the area and circumference of a circle.
@dumdumFlame

In the example you're showing, void IS NOT really returning a value, but you are passing the variable to the function and changing its value, as Jonnin is showing you.

Also are you sure I shouldn't be using a float for some of the things like maybe r and h?

I did mention you should be using double.

And yes, you can use setprecision(), if you're needing it.
Hello dumdumFlame,

Based on everything that has been said and the instructions this is what I came up with:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cctype>  // <--- Added.
#include <iostream>
#include <iomanip>  // <--- Added.
#include <string>

using namespace std;

void scalc(double& surface_area, double radius, double height);
void vcalc(double& volume, double radius, double height);
void output_calc(double surface_area, double volume, double radius, double height);

const double PI = 3.141592654;

int main()
{
	double surface_area{}, volume{}, height{}, radius{};
	char ans{};

	do
	{
		cout << "\n Enter can radius (In inches): ";
		cin >> radius;

		cout << " Enter can height (In inches): ";
		cin >> height;

		scalc(surface_area, radius, height);

		vcalc(volume, radius, height);

		output_calc(surface_area, volume, radius, height);

		cout << "\n Enter another (y or n)?";
		cin >> ans;
	} while (std::toupper(ans) == 'Y');  // <--- Same as next line just a different way.
	//while (ans == 'y' || ans == 'Y');  //Ik it says if not N then return 0 but it only works this way. An Alternative
	//  checking eithe case for "y" continues the program. Anything else ends the program.
	
	return 0;
}

void scalc(double& surface_area, double radius, double height)
{
	surface_area = (2 * PI * radius * height) + (2 * PI * radius * radius);
	//return surface_area;
}

void vcalc(double& volume, double radius, double height)
{
	volume = PI * radius * radius * height;
	//return volume;
}

void output_calc(double surface_area, double volume, double radius, double height)
{
	std::cout << std::fixed << std::setprecision(4);  // <--- Added.

	cout << "\n Can surface area " << surface_area << " Square inches" << endl;
	cout << " Can volume = " << volume << " cubic inches" << endl;

}


Given the example input you provided the output looks like this:
                                       // This first blank line is intentional.
 Enter can radius (In inches): 3.5
 Enter can height (In inches): 6.5

 Can surface area 219.9115 Square inches
 Can volume = 250.1493 cubic inches

 Enter another (y or n)?y

 Enter can radius (In inches): 10.1
 Enter can height (In inches): 22.9

 Can surface area 2094.1857 Square inches
 Can volume = 7338.8515 cubic inches

 Enter another (y or n)?3



This is a little thing, but helps;
1
2
3
double& surface_area
double & surface_area
double &surface_area

You are free to choose any style you like, but be consistent in its use. Changing the style will cause a reader to pause, hesitate or stumble with the difference.

In your functions "scalc" and "vcalc" you have defined the variable "PI". This becomes a local variable to the function and overshadows the global variable at the top of the code. The point of the global variable is so you can use it anwhere in the program with having to redefine it.

The changes I made to the output just makes it easier to read when all the lines are not run together. See what you think.

Unless changed a "cout" will only print 6 numbers. That means the larger the number is on the lhs of the decimal point the less numbers on the rhs. By including the header file "<iomanip" and using "setpersision" as I did on line 56 your numbers will print better. The "std::fixed" tells the "cout" not to use "scientific" notation, but actual numbers.

One thing to keep in mind is that even a "double" does not store a decimal number correctly. Should you say "height = 0.3" the number would be stored as "0.29999999999999999". Not usually a problem until you use it in a calculation and find your answer off a bit.

Should you change the "setprecision" to 2 you will find that in the second example the surface area would be 2094.19 because of the rounding.

In the "output_calc" function there is no reason to pass the first 2 variables by reference. You are not changing these values just printing them out. Passing these variables by value is sufficient.

Andy
@dumdumflame

A few small changes to what you had originally along with a couple of simplifications and two different ways to output the results.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

using namespace std;

double calculate_surface_area(int r, int h);
double calculate_volume( int r, int h);

void output_calculations_version_1(double, double);
void output_calculations_version_2(int, int);

const double PI = 3.141592654;

int main ()
{
    int h{0}, r{0};
    double surface_area{0}, volume{0};
    
    char ans;
    
    do {
        cout<<"Enter can radius (inches): ";
        cin>> r;
        cout<<"Enter can height (inches): ";
        cin>>h;
        
        surface_area = calculate_surface_area(r,h);
        volume = calculate_volume(r,h);
        
        // 1 WAY TO DO IT
        output_calculations_version_1(surface_area, volume);
        
        // ANOTHER WAY
        output_calculations_version_2(r, h);
        
        cout<<"Enter another (y or n)?";
        cin>>ans;
    }
    while (ans !='n' && ans != 'N');
    return 0;
}

double calculate_surface_area(int r, int h)
{
    return (2* PI * r * h) + (2 * PI * r * r);
}

double calculate_volume(int r, int h)
{
    return PI * r * r * h ;
}


void output_calculations_version_1(double surface_area, double volume)
{
    cout<< "** VERSION 1\n";
    cout<<"Can surface area "<< surface_area << " Square inches"<< endl;
    cout<<"Can volume = " << volume<< " cubic inches"<< endl;
}

void output_calculations_version_2(int r, int h)
{
    cout << "** VERSION 2\n";
    cout<<"Can surface area "<< calculate_surface_area(r,h) << " Square inches"<< endl;
    cout<<"Can volume = " << calculate_volume(r,h)<< " cubic inches"<< endl;
}


Enter can radius (inches): 1
Enter can height (inches): 2
** VERSION 1
Can surface area 18.8496 Square inches
Can volume = 6.28319 cubic inches
** VERSION 2
Can surface area 18.8496 Square inches
Can volume = 6.28319 cubic inches
Enter another (y or n)?n
Program ended with exit code: 0
Last edited on
@Handy Andy. Thank you so much for helping me solve this problem!
I have a couple questions though:
Why did you need to add <cctype>?
You added <Iomanip> because of the std:: right?
Why did you put {} in the doubles and stuff?
Why and what is the toupper why did you change it?
This is a little thing, but helps;
1 double& surface_area
2double & surface_area
3double &surface_area
what do you mean by this?^

@Againtry
Thank you for you input as well. I appreciate you helping me out.
Again what is the point of the {0}?

Here is the final product I am going to go with: http://cpp.sh/6gtgu
Thanks all!
Last edited on
It’s always good practice to initialize variables. {0} is equivalent to =0
int h {0}; is an example of "uniform initialization." Something C++11 added to the tool-kit.
https://www.geeksforgeeks.org/uniform-initialization-in-c/

If initializing a variable to a default value, such as zero for an int, I prefer omitting the value: int h { };.
Hello dumdumFlame,

"<cctype>" is the header file for the functions "std::tolower()" and "std::toupper()" along with some other useful functions. http://www.cplusplus.com/reference/cctype/

You added <Iomanip> because of the std:: right?
Partly for "std::right", but mostly for "std::setprecision". "std::right" is the default justification when you first use "iomanip" for output with "std::setw()" unless you change it.

The "std::setw()" creates a block of space to put the output, or variable, used in. Any unused space is padded with spaces, but even that can be changed if needed.
http://www.cplusplus.com/reference/iomanip/

The {}s are known as the uniform initializer and available from C++11 on. An empty set of {}s will set a "char" to "\0", integer types to (0) zero and floating point types to "0.0". Used with an array it will set every element of the array to the type of zero based on the type of the array. You also have the ability to put something inside the {}s to give the variable a value other than zero. This also works with "std::string"s if it needs to be initialized. Otherwise the "string"when defined has no value until you use it.

The use of "std::tolower()" and "std::toupper()" is to change the case of a single character. This is easier than having to write if (choice == 'y' || choice == 'Y'). If the character tested is in the proper case then nothing happens otherwise the case is changed.

The 3 lines of code just show different ways of passing a variable by reference. To the compiler all work the same, but to someone reading the code it is better to pick one method and stick with it. The same is true when using {}s for functions, if statements, loops. It is best to pick a style and use it consistently. https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements

Be careful when using a link like http://cpp.sh/... I am not sure, but it may only last about 24 hours before it is deleted.

Andy
Topic archived. No new replies allowed.