#include <iostream> // for I/O
#include <iomanip> // for formatting output
#include <cmath> // for using pow function
usingnamespace std;
//global constants
constfloat PI = 3.14159; // Approximation of PI
void displayResults()
{
float radius;
float height;
float volume;
float surfaceArea;
cout << "RESULTS:" << endl;
cout << "For a cone with a radius of " << radius << " meters and a height of "
<< height << " meters" << endl << endl;
cout << "Cone Volume: " << setw (15) << volume << endl;
cout << "Cone Surface Area: " << setw (9) << surfaceArea << endl << endl << endl;
}
int main()
{
float radius;
float height;
float volume;
float slantHeight;
float surfaceArea;
cout << endl;
cout << setw (65) << "Computation of Surface Area and Volume of a Cone." << endl;
cout << setw (41) << "By" << endl;
cout << setw (49) << "Amanda M Densler" << endl << endl << endl;
cout << fixed << showpoint << setprecision (1);
cout << "Cone Calculations" << endl;
cout << endl;
cout << "Enter cone radius (in meters): ";
cin >> radius;
cout << "Enter cone height (in meters): ";
cin >> height;
cout << endl;
cout << endl << endl;
volume = (((PI / 3) *pow(radius, 2)) * height);
slantHeight = sqrt((pow(radius, 2)) + (pow(height, 2)));
surfaceArea = ((PI * pow(radius, 2)) + (PI * radius * slantHeight));
displayResults();
system("PAUSE");
return EXIT_SUCCESS;
}
I assumed I had everything declared correctly, and that the user-defined function displayResults was called correctly, but when compiled and run the program only outputs the height and 0.0 for everything else.
1) Global variables. You have none of these.
2) Variables passed in as parameters. You also have none of these.
3) Variables declared locally (which is what you're doing), which exists only inside that function and have no connection to anything outside the function.
int main ()
{
float diameter;
float height;
float radius;
float volume;
cout << "This program computes the cone's volume:" <<endl;
cout << "Please enter the diameter of the cone: " <<endl;
cin >> diameter;
cout << "Please enter the height of the cone: "<<endl;
cin >> height;
For simple things, a good, general rule is that if it's inside braces "{}" then it is ONLY in the braces. This rule has exceptions of course, but until you know the exceptions (such as new) then i would stick to that rule.
For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int x = 5;
int y = 10; //x and y are now global variable and will be available for every
//function below UNLESS it is initialized within (such as x below)
int main()
{
int x = 3;
{
int z = 2; //start scope of z
} //end scope of z (z is now deleted)
std::cout << x; //prints 3
std::cout << y; //prints 10
std::cout << z; //ERROR not in scope
return 0;
}
Also, a warning. If you use System("pause") on this forum, many people will yell at you. It is not recommended for many reasons (look at the sticky post in beginners for said reasons). There are many alternatives such as cin.ignore(numeric_limits<streamsize>::max(), '\n'). make sure you #include <limits>. Also note that if you can't remember the first parameter to cin.ignore, a large number should do the job.