C++ program not outputting what i expected...help?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#include <iostream>                    // for I/O
#include <iomanip>                     // for formatting output
#include <cmath>                       // for using pow function
using namespace std;

//global constants
const float 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.
Last edited on
The variables
1
2
3
4
   float radius;
    float height;
    float volume;
    float surfaceArea;


inside the function displayResults
are NOT the same objects as the variables
1
2
3
4
5
float radius;
    float height;
    float volume;
    float slantHeight;
    float surfaceArea;

in the function main.

Any given function sees the following variables:

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.
Last edited on
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include <iostream>                    // for I/O
#include <iomanip>                     // for formatting output
#include <cmath>                       // for using pow function
using namespace std;

// global constants
const float PI = 3.14159;          // Approximation of PI

// global variables
    float radius;
    float height;
    float volume;
    float slantHeight;
    float surfaceArea;
    
void displayResults()
{        
    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 << " cubic meters" << endl;
    
    cout << "Cone Surface Area: " << setw (9) << surfaceArea << " square meters" << endl << endl << endl;    
}

int main()
{     
    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;
}


Now I have them set as global variables, but I was requested to have "four input parameters, radius, height, surface area, and volume"

I guess I am not understanding what he means by having input parameters...
Last edited on
I think I understand now...

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <iostream>                    // for I/O
#include <iomanip>                     // for formatting output
#include <cmath>                       // for using pow function
using namespace std;

// global constants
const float 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 << " cubic meters" << endl;
    
    cout << "Cone Surface Area: " << setw (9) << surfaceArea << " square meters" << 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(radius, height, volume, surfaceArea);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const float PI = 3.14159265;


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;

radius = diameter / 2;
volume = (PI/3 * pow(radius, 2) * height);

cout << "The volume of the cone is " << setprecision(4)
<< volume << "." << endl;

return 0;


}
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.
Last edited on
Topic archived. No new replies allowed.