By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

Urgent help on errors on variable definitions (declarations) and arithmetic operation

P: 1
This is a code to do for extra practice before we go to our lab tomorrow morning. Our professor said if we can get through this practice example, the lab shouldn't be too difficult tomorrow. However, I keep running into errors (mainly syntax) or being told that I have illegal characters. Any and all help is much appreciated.

/* Name
Section 001R
Program to calculate the volume of a whole propane tank
by adding the volume contained in the cylinder and the
volume contained in each of the hemispheres.

input: length of cylinder and the radius of the cylidner
output: volume of the whole tank
processing: volume = pi*r^2*l + 2(2/3(pi*r^3)). Pi is 3.14159

#include <iostream>
#include <iomanip>

using namespace std;

int main()

//Constant for PI
const double PI = 3.14159;

// Variables
long length, // length of the cylinder
radius, // radius of the cylinder
volume, // volume of the propane tank

// Set the desired output formatting for numbers
; cout << fixed << setprecision(4);

// Prompt the user for the cylinder's length and radius.
cout << "Enter the units of these dimensions :\n";
cin >> meters;
cout << "Please enter length and radius ";
cin >> length >> radius;

//Calculate the propane tank's volume.
volume = PI * radius * radius * length + 2 * (2 / 3)*PI*radius * radius * radius

//Display the calculated data.
; cout << "The volume of the propane tank is ";
cout << volume << "cubic meters.\n";
return 0;
Jan 25 '18 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 9,197
Like you say, several syntax errors. I got the code to compile and link but did not run it. Please note my comment where I made changes. Good luck in your lab.

Here is your corrected code:

Expand|Select|Wrap|Line Numbers
  1. /* Name
  2. Section 001R
  3. Program to calculate the volume of a whole propane tank
  4. by adding the volume contained in the cylinder and the
  5. volume contained in each of the hemispheres.
  7. input: length of cylinder and the radius of the cylidner
  8. output: volume of the whole tank
  9. processing: volume = pi*r^2*l + 2(2/3(pi*r^3)). Pi is 3.14159
  10. */
  12. #include <iostream>    
  13. #include <iomanip>
  15. using namespace std;
  17. int main()
  19. {
  20.     //Constant for PI
  21.     const double PI = 3.14159;
  23.     // Variables
  24.     double length; // length of the cylinder   //WFC: Don't mix integer and floating point arithmetic. I picked double to be consisten with PI
  25.     double    radius; // radius of the cylinder //WFC: no comments allowed in a comma separated list.
  26.     double    volume; // volume of the propane tank
  27.         double meters;    //WFC: meters never defined. I added it
  28.         // Set the desired output formatting for numbers
  29.         cout << fixed << setprecision(4);     //WFC: removed leading ;
  31.     // Prompt the user for the cylinder's length and radius.
  32.     cout << "Enter the units of these dimensions :\n";
  33.     cin >> meters;
  34.     cout << "Please enter length and radius ";
  35.     cin >> length >> radius;
  38.     //Calculate the propane tank's volume.
  39.     volume = PI * radius * radius * length + 2 * (2 / 3)*PI*radius * radius * radius
  41.         //Display the calculated data.
  42.         ; cout << "The volume of the propane tank is ";
  43.     cout << volume << "cubic meters.\n";
  44.     return 0;
  45. }
Jan 26 '18 #2

Expert 100+
P: 2,398
My line numbers refer to WFC’s listing.

Might as well remove lines 27, 32, 33 because you don’t use the meters variable. Or you might decide to use it. My best guess is that you want it to be a string that you print on line 43. In that case you might change the name to units.

You should clarify in line 34 what you mean by length. Your code assumes it is the length of the cylindrical portion of the tank. That’s ok, but the user might think it is the end-to-end length of the entire tank. Line 7 makes clear what you’re doing but the user can’t see the comment.

You should verify the numeric inputs are valid. Print an error message and reprompt for inputs if either value is less than or equal to zero.

Lines 39 and 42: it is legal but unusual to put the command-terminating semicolon at the start of the next line.

I program for embedded systems. I would make the following additional changes to accommodate my resource poor environment.
  1. Define PI as a static const double. Doing so puts PI in program memory instead of in RAM.
  2. Factor out PI*r^2 from your equation on line 39. Here I’m just being obsessive compulsive; a good optimizing compiler might well do that for you behind the scenes.
Jan 26 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.