double volume(double r,double h) {
double v=PI* pow(r, 2.0)* h;
return v;
}
int main()
{
cout << "Project 1\n";
cout << "We will be doing some math on numbers you give us to find Total surface area, lateral surface area, and volume of a cylinder!\n";
cout << "Please enter the radius and height\n";
cin >> r, h;
cout << surfaceArea(r,h)<<" is the surface area"<<endl;
cout << lateralSurfaceArea(r,h)<<" is the lateral surface area\n";
cout << volume(r,h)<< " is the volume\n";
}
#include <iostream>
#include <cctype>
usingnamespace std;
constdouble PI = 3.14159;
double surfaceArea(double r, double h) {
return 2 * PI * r * (r + h);
}
double lateralSurfaceArea(double r, double h) {
return 2 * h * PI * r;
}
double volume(double r, double h) {
return PI * r * r * h;
}
int main()
{
double r;
double h;
cout << "Project 1\n";
cout << "We will be doing some math on numbers you give us to find Total surface area, lateral surface area, and volume of a cylinder!\n";
cout << "Please enter the radius and height\n";
cin >> r >> h;
cout << surfaceArea(r, h) << " is the surface area\n";
cout << lateralSurfaceArea(r, h) << " is the lateral surface area\n";
cout << volume(r, h) << " is the volume\n";
}
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.
Your code needs some blank lines. How can you tell what it is doing or where a problem is if you can find it.
#include <iostream>
#include <cmath>
#include <cctype>
usingnamespace std;
constdouble PI = 3.14159;
//double r; // <--- Moved to "main".
//double h;
double surfaceArea(double r, double h)
{
double SA = 2 * PI * r*(r + h);
return SA;
}
double lateralSurfaceArea(double r, double h)
{
double LSA = 2 * h* PI * r;
return LSA;
}
double volume(double r, double h)
{
double v = PI * pow(r, 2.0)* h;
return v;
}
int main()
{
double radius{};
double height{};
cout << "Project 1\n";
cout << "We will be doing some math on numbers you give us to find Total surface area, lateral surface area, and volume of a cylinder!\n";
cout << "Please enter the radius and height\n";
cin >> radius >> height;
cout << surfaceArea(radius, height) << " is the surface area" << endl;
cout << lateralSurfaceArea(radius, height) << " is the lateral surface area\n";
cout << volume(radius, height) << " is the volume\n";
return 0; // <--- Not required, but makes a good break point.
}
In line 39 the comma operator is not the way to use the "cin". Only the first value receives a value. Also it is a good idea to initialize your variables when defined.
Use cin >> r >> h;. When chaining more than 1 input or output you need to separate the variables with the extraction operatoe ">>" or insertation operator "<<".
Try to avoid using global variables unless they start with"constexpr" or "const". It is to easy for any line of code that follows to change a global variable and difficult to track down where it went wrong.
Just a note: you define global variables the the functions have access to, yet you pass them to the functions from "main". The only advantage to doing this is that the variables defined in the function are local to the function, and destroyed when the function ends, and they are a copy of the global variables. Any change to a local variable does not change the original variable.
For the line double v=PI* pow(r, 2.0)* h; it is much easier and more often suggested to avoid using the "pow" function in favor of double v = PI * r * r * h;.