Help with C++ program that calculates Volume and Surface area (intro to C++ assignment)

In this assignment I am to use the given statements in the incorrect order and rearrange them in the correct order. This is what I have come up with and I will list the errors as well.

#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;
const double PI = 3.14159;

int main()
{
double Radius, Height, Volume, Surface_Area

//Output
cout << "Enter the height of the cylinder: ";
cin >> Height;

cout << fixed << showpoint << setprecision(2);

cout << "Enter the radius of the base of the cylinder: ";
cin >> Radius;

Volume = PI * pow(Radius, 2.0) * Height;
Surface_Area = 2 * PI * Radius * Height + 2 * PI * pow(Radius, 2.0);

cout << "Volume of the cylinder = " << Volume;
cout << "Surface area: " << Surface_Area;

return 0;
}


These are my errors:
The errors start on the first cout statement asking for the height.

main.cpp:12:3: error: expected initializer before ‘cout’
cout << "Enter the height of the cylinder: ";
^~~~
main.cpp:20:3: error: ‘Surface_Area’ was not declared in this scope
Surface_Area = 2 * PI * Radius * Height + 2 * PI * pow(Radius, 2.0);
^~~~~~~~~~~~
/bin/bash: line 4: ./a.out: No such file or directory


Hello,

For future posts please consider to place your code within the code tags.
For this problem, check your line endings. There is a ; missing after you declare your doubles.

Kind regards, Nico
Before I made this post I had a very different layout and was getting other errors and I just scrapped the whole thing and started again and I made this. Turns out that was just me being dumb and that was the most simple error in the whole program. Works just fine now, thank you for the catch.
Hello ccavanaugh18,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



If your IDE is indenting properly you should have seen this:
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
//  Help with C++ program that calculates Volume and Surface area ccavanaugh18 272979

#include <iomanip>
#include <cmath>
#include <iostream>

using namespace std;

const double PI = 3.14159;

int main()
{
    double Radius, Height, Volume, Surface_Area

        //Output
        cout << "Enter the height of the cylinder: ";
    cin >> Height;

    cout << fixed << showpoint << setprecision(2);

    cout << "Enter the radius of the base of the cylinder: ";
    cin >> Radius;

    Volume = PI * pow(Radius, 2.0) * Height;
    Surface_Area = 2 * PI * Radius * Height + 2 * PI * pow(Radius, 2.0);

    cout << "Volume of the cylinder = " << Volume;
    cout << "Surface area: " << Surface_Area;

    return 0;
}

Notice how lines 15 and 16 are indented when they should not be. A quick indication that something is missing or something went wrong before these lines.

Fixing this one error can eliminate several errors that follow.

For line 24 you should use Volume = PI * (Radius * Radius) * Height;. The "pow" function is useful, but for a square or a cube you can just do this.

Andy
If you factorise the algebra, you get:

V = (PI * r)(r * h)
A = (PI * r)(2)(r + h)

So to simply the calculation, you can say

PR = (PI * r)
V = PR(r * h)
A = PR(2)(r + h)

which when converted into code becomes:

1
2
3
4
5
const double PR = PI * Radius;

Volume = PR * Radius * Height;

Surface_Area = PR * 2.0 * (Radius + Height);

Last edited on
Topic archived. No new replies allowed.