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
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.
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.
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:
// Help with C++ program that calculates Volume and Surface area ccavanaugh18 272979
#include <iomanip>
#include <cmath>
#include <iostream>
usingnamespace std;
constdouble 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.