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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maria33
    New Member
    • Jan 2018
    • 1

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

    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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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:

    Code:
    /* 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
    	double length; // length of the cylinder   //WFC: Don't mix integer and floating point arithmetic. I picked double to be consisten with PI
    	double	radius; // radius of the cylinder //WFC: no comments allowed in a comma separated list.
    	double	volume; // volume of the propane tank
    		double meters;    //WFC: meters never defined. I added it
    		// Set the desired output formatting for numbers
    		cout << fixed << setprecision(4);     //WFC: removed leading ;
    
    	// 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;
    }

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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.
      Last edited by donbock; Jan 26 '18, 02:51 AM. Reason: Added suggestion to range-check the inputs.

      Comment

      Working...