Please review my code before I submit

Hey everyone, my professor has taken off points from my code in the past for things like not enough comments, or for choosing the wrong names for certain variables, so I was hoping someone could review my code before I submit for a grade. Thanks so much.

//Cone Volume Calculator Program
//Intro C++, Lesson 10
//Written by DeQuawn Johnson, 11/8/2017
#include <iostream>
using namespace std;

//function prototype
double computeConeVolume(double radius, double height);

int main ()
{
//declare variables
//input
double coneRadius = 0;
double coneHeight = 0;

//output
double coneVolume = 0;

//Prompt user for input values
cout << "Enter the cone's radius: ";
cin >> coneRadius;
cout << "Enter the cone's height: ";
cin >> coneHeight;

//call function
coneVolume = computeConeVolume(coneRadius, coneHeight);

//display the resulting value
cout << endl << "The cone's Volume is: " << coneVolume << endl;
system ("pause");
return 0;


} // end of main

double computeConeVolume (double radius, double height)
{ //This is a user defined function that calculates the volume a cone
//initialize
double volume = 0;
//constant
const double PI = 3.1415;
//Calculate Volume
volume = 0.3333*PI*radius*radius*height;
//End the calculation back
return volume;
} // end of computeConeVolume
This looks fine to me(according to what you have specified your professor wants)

The variable names are clear as they state and follow their intention. Although, I'm not sure why your professor would then insist that you clutter and bloat your code with comments considering how code structure and good variable/method names are to serve most of that purpose. There was a good article on this topic if you wanted to read it, https://blog.codinghorror.com/coding-without-comments/ and https://blog.codinghorror.com/code-tells-you-how-comments-tell-you-why/

Anyways, the code follows what you have stated your professor wants.
Also next time use code tags in the format section to make your code look nicer.
Last edited on
I can't tell if you used formatting, because you didn't use code tags.
To me this looks great, except the formatting, which I would recommend. See below

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
//Cone Volume Calculator Program
//Intro C++, Lesson 10
//Written by DeQuawn Johnson, 11/8/2017
#include <iostream>
using namespace std;

//function prototype
double computeConeVolume(double radius, double height);

int main ()
{
	//declare variables
	//input
	double coneRadius = 0;
	double coneHeight = 0;
	
	//output
	double coneVolume = 0;
	
	//Prompt user for input values
	cout << "Enter the cone's radius: ";
	cin >> coneRadius;
	cout << "Enter the cone's height: ";
	cin >> coneHeight;
	
	//call function
	coneVolume = computeConeVolume(coneRadius, coneHeight);
	
	//display the resulting value
	cout << endl << "The cone's Volume is: " << coneVolume << endl;
	system ("pause");
	return 0;
} // end of main

double computeConeVolume (double radius, double height)
{ //This is a user defined function that calculates the volume a cone
	//initialize
	double volume = 0;
	//constant
	const double PI = 3.1415;
	//Calculate Volume
	volume = 0.3333*PI*radius*radius*height;
	//End the calculation back
	return volume;
} // end of computeConeVolume 
Since this is C++ you may want to declare your variables closer to first use instead of one big clump at the beginning of scope or perhaps just eliminate some of the variables that aren't really needed. For example in your computeConeVolume() function:

1
2
3
4
5
6
7
8
// This is a user defined function that calculates the volume a cone.
// Returns the volume of the code defined by radius and height.
double computeConeVolume(double radius, double height)
{
    const double PI = 3.1415;
    // Calculate and return the volume of the cone.
    return (0.3333 * PI * radius * radius * height);
} // end of computeConeVolume 


Note: you may want to define a constant for that magic number 0.3333 with a meaningful name to help document that magic number.

Edit: You may also want to more fully describe the formula in the comment above the function.

Edit 2: You should also be careful of overly long function and variable names as these long names can obscure the actual purpose of the items. IMO, your function name is right at the point of being too long, it could just as well be named coneVolume(), since in this content the function call itself will denote "compute".
Last edited on
If you have been instructed to use those specific values for PI and 1/3, then stay with what you have. However, the result is displayed to 6 significant digits, but only the first 4 digits are accurate.

e.g radius = 10, height = 10.
Displayed result: 1047.06
Expected result: 1047.2

A simple way to improve on that is to divide by 3.0 rather than multiply by the approximate value.
Last edited on
Hey, everyone. Thanks so much for your help.

@rabster, I've always read that too many comments can bloat code, but my prof seems to like them unfortunately.

@SamuelAdams, I'm sure you can tell I'm a total noob to both C++ and the forum, I had no idea code tags even existed haha. Thanks for showing me that.

@jlb, I totally agree, but the first time I declared variables closer to first use the prof took off points, I'll probably do that going forward, but not in this class haha.

@Chervil, my proff used those specific values in his sample program, so I will keep them, but i appreciate the suggestion. I will definitely keep that in mind for future code outside this class.
Registered users can post here. Sign in or register to post.