Program wont function

I know I didn't use pow, because I'm unsure of where I would put it. The function of this is to get it to tell me the volume of a cylinder, but I keep encountering an error, for someone reason when it reaches the formula, it has an error.

> Failed to declare correct value
> Wrong use of formula (( pi * radius^2 * height) = volume )
> Variable radius, height, volume wasn't declared
> Failed to produce the correct volume output


I'm thinking of changing the formula that I have currently to:

volume = PI*pow(radius,2)*height

Does anyone have a clue if that's the only thing that needs to be fixed?

EDIT: I've realized that my main error was using int instead of float. I'm switching that out and editing the code, but I'm still getting an error because of my cout. I need it to say:

"The volume of a cylinder with radius 8.67 and height 25.85 is 6104.47"

I'm gonna try working on it, but if anyone has any suggestions feel free to let me know.


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
#include <iostream>
#include <cmath>   // needed for pow

using namespace std;

const double PI = 3.14159;


int main()
{
       
    float radius;
    float height;
    float volume;
    
     // assigned values to variables    
    radius = 8.67;
    height = 25.85;
    
    volume = PI * radius^2 * height;
    
    cout << volume;
    endl;

    return 0;
}
Last edited on
I've gotten up to here, but for some reason it's still not working, it wont print out anything. Does anyone have any suggestions?

Edit: I deleted "endl;", since it kept giving me an error. Now it prints, but it doesn't include spaces, going to add some missing spaces now.

Edit: This is the code I've gotten to so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>   // needed for pow

using namespace std;

const double PI = 3.14159;

int main()
{
       
    float radius;
    float height;
    float volume;
    
     // assigned values to variables    
    radius = 8.67;
    height = 25.85;
    
    volume = PI*pow(radius,2)*height;
    
    cout << "The volume of a cylinder with radius " << radius << " and height " << height << " is " << volume;

    return 0;
}
Last edited on
PI * radius^2 * height doesn't do what you think it does. In C++ ^ is bit wise exclusive OR, not exponentiation. Also, it has lower precedence than *, so the statement is the same as (PI * radius) ^ (2 * height)

If you're just squaring a value then it's best to simply multiply it by itself: PI * radius * radius * height

You should use more digits for PI.

Line 23 doesn't do anything. Remove it and change line 22 to cout << volume << '\n';.

I always use double instead of float. If I recall correctly, the X86 processors do all floating point arithmetic using 80 bit values, so it doesn't save any runtime. And since RAM is so abundant these days, there's rarely a reason to save the extra space. So use the higher precision.


I came to terms that "return 0;" was 100% useless, I managed to fix this without help. Slightly proud of myself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>   // needed for pow

using namespace std;

const double PI = 3.14159;

int main()
{
       
    float radius;
    float height;
    float volume;
    
     // assigned values to variables    
    radius = 8.67;
    height = 25.85;
    
    volume = PI*pow(radius,2)*height;
    
    cout << "The volume of a cylinder with radius " << radius << " and height " << height << " is " << volume;
}
Topic archived. No new replies allowed.