volume of cyliner --- "ERROR"

write a program that computes value of a cylinder with PI= 3.1417...

this is my code .. this is not working properly .. could you please check it


// volume of cylinder

#include<iostream>
using namespace std;

int main()
{

float R,H,volume,PI=3.1417;
char unit [1];
cout<<"Enter the Radius of the cylinder....";
cin>> R;
cout<<"Enter the Height of the cylinder";
cin>>H;

volume= PI*R*R*H;
cout<< "Enter the UNIT of measurements ..."<<endl<<"PRESS 1 for cm^3"<<endl<<"PRESS 2 for m^3"<<endl<<"PRESS 3 for mm^3"<<endl<<"PRESS 4 for km^3";
cin>>unit;
if( unit == "1")
cout<<"Volume of the Cylinder is ..."<<volume<<"cm^3"<<endl<<"Programs ends here ";
else if( unit== "2")
cout<<"Volume of the Cylinder is ..."<<volume<<"m^3"<<endl<<"Programs ends here ";
else if( unit== "3")
cout<<"Volume of the Cylinder is ..."<<volume<<"mm^3"<<endl<<"Programs ends here ";
else if( unit== "4")
cout<<"Volume of the Cylinder is ..."<<volume<<"km^3"<<endl<<"Programs ends here ";
else
cout<<"CHOOSE CORRECT OPTION...";

return 0;
}
Hello ayesha1122,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

At the beginning these include files may be of more use:
1
2
3
4
#include<iostream>
#include <iomanip>  // <--- For setw and setprecision.
#include <limits>
#include <cmath>  // <--- For PI defined as M_PI. 


Next "double" would be better than a "float". You should initialize all your variables not just PI. A better value for PI is "3.14159" what you have could give you a bad result when used.

char unit[1] may have sounded good when you started, but it is redundant because "char" by its-self will hold only one character which is all you need. Then later you are not using "unit" as an array, but a single character.

You need to make unit a single character not an array. Then in the if statements change the double quotes to single quotes.

In your if statements you are asking the user to choose how the volume will be output, but in the outputs you are printing the same thing foe each. You still need to take the number in "volume" and change ti to match what you need. I have not worked on that part yet.

This will give you an idea of what I have done. I have added some useful code and added some new line characters to the "cout" statements to change the output. See what you think:
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
#include<iostream>
#include <iomanip>  // <--- For setw and setprecision.
#include <limits>
#include <cmath>  // <--- For PI defined as M_PI.

//using namespace std;

int main()
{

	double R{}, H{}, volume{}, PI = M_PI;
	char unit{};

	std::cout << "Enter the Radius of the cylinder....: ";
	std::cin >> R;
	std::cout << "Enter the Height of the cylinder...: ";
	std::cin >> H;

	volume = PI*R*R*H;

	std::cout << "\nEnter the UNIT of measurements ..." << std::endl << "PRESS 1 for cm^3" << std::endl << "PRESS 2 for m^3" << std::endl << "PRESS 3 for mm^3" << std::endl << "PRESS 4 for km^3" << "\nEnter choice: ";
	std::cin >> unit;

	std::cout << std::fixed << std::showpoint << std::setprecision(6);
	
	if (unit == '1')
		std::cout << "\nVolume of the Cylinder is ... " << volume << " cm^3" << std::endl << "\n\nPrograms ends here ";
	else if (unit == '2')
		std::cout << "Volume of the Cylinder is ... " << volume << " m^3" << std::endl << "\n\nPrograms ends here ";
	else if (unit == '3')
		std::cout << "Volume of the Cylinder is ... " << volume <<  "mm^3" << std::endl << "\n\nPrograms ends here ";
	else if (unit == '4')
		std::cout << "Volume of the Cylinder is ... " << volume << " km^3" << std::endl << "\n\nPrograms ends here ";
	else
		std::cout << "\nCHOOSE CORRECT OPTION...";

	// This next line may not be needed. If you have to press Enter to see the prompt then comment out the
	// next line. If not you will need this to clear the input buffer first.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << " Press Enter to continue: ";
	std::cin.get();

	return 0;
}


Hope that helps,

Andy
thanks alot :)
Topic archived. No new replies allowed.