I am writing a program that calculates the volume of a cylinder when you input the diameter and height, using the formula 1/4height3.14*diameter^2

However the issue is, whenever I do that, the volume always comes out to be 0 when I run the program.

#include<iostream>
using namespace std;
int main()
{
float diameter,height,volume;
cout<<"Enter Diameter of Cylinder : ";
cin>>diameter;
cout<<"Enter Height of Cylinder :";
cin>>height;
volume=1/4*height*3.14*diameter^2;
cout<<"Volume of Cylinder is : " <<volume;
return 0;
}

Recommended Answers

  1. Don't add tags that don't apply.
  2. The ^ would be for Integers.

I did not check the results of this quick edit to follow:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float diameter,height,volume;
    cout<<"Enter Diameter of Cylinder : ";
    cin>>diameter;
    cout<<"Enter Height of …
Jump to Post

All 2 Replies

  1. Don't add tags that don't apply.
  2. The ^ would be for Integers.

I did not check the results of this quick edit to follow:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float diameter,height,volume;
    cout<<"Enter Diameter of Cylinder : ";
    cin>>diameter;
    cout<<"Enter Height of Cylinder :";
    cin>>height;
    volume=1.0/4.0 * height * 3.14 * pow(2.0, diameter);
    cout<<"Volume of Cylinder is : " <<volume;
return 0;
}

The reason you get 0 for an answer is in the constant expression 1/4. That's division of two int values and the fractional quotient is truncated to the next integer closer to zero. In this case, that is zero. And 0 times what follows is zero.

Another problem is attempting to use ^ as a "raised to the power" operator. There is no such operator in C++. Many will tell you to use pow() from the <cmath> library, but that's not really very sensible for small postive integer powers, and overkill for computing a square. The pow operator computes pow(x,y) = exp(y*log(x), doing two trancendental function calculations and a multiply, when all that's needed to square a number is a single multiplication.

One minor bit: The float type is often a poor choice. For individual variables, you don't save much memory and on most processors with hardware floating point support you don't save much time. Using float can be a great choice for large arrays, but I suggest you develop the habit of using double as your default floating point type and use float only when you have a specific reason to.

Using those ideas, plus a couple of more, gets to something like:

#include <iostream>
using namespace std;

const double PI = 3.14; // should be more digits, but maybe the assignment said to use 3.14

int main()
{
    double diameter,height,volume;
    cout << "Enter Diameter of Cylinder : ";
    cin >> diameter;
    cout << "Enter Height of Cylinder :";
    cin >> height;
    volume = PI / 4.0 * height * diameter * diameter;  // 1/4 pi h d^2
    cout << "Volume of Cylinder is : " <<volume;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.