How to take input from users?

I am making a circumference finder program by multiplying the diameter of circle with PI. But I want to take the input of diameter from the user. Can you figure out how to do that? Also, I want the answer as a float or double (I mean with decimalS) but answer comes in integer (without decimals) form.

Code -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{

const double PI = 22/7;

cout<<"Circle Circumference Finder!"<< endl;
cout<<"Plz enter the diameter of the circle"<< endl; // Consider the input value as x
cout<<"The circumference of the circle is: "<<(PI * x) << endl;

return 0;

}
Last edited on
On line 7, you are doing an integer division, which rounds out the remainder before PI recieves it. Its better normally to just define PI in the normal way (i.e., const double PI = 3.1415926535897932 or something like that). Also, to input, just make a double x and use std::cin to input into the variable, and then you can multiply them together.
1
2
3
4
double x;
cout << "Plz enter the diameter of the square: "
cin >> x;
cout << endl;


http://www.cplusplus.com/doc/tutorial/basic_io/

Also, so the output is integer, you can simply cast to int.
1
2
double result = PI * x;
cout << "The circumference of the circle is: "<< (int)result << endl;
Last edited on
Oh yes it works. Thanks!

Cheers >>
Last edited on
Topic archived. No new replies allowed.