Formatting PI

Hey guys, I'm working on a project that requires the user to determine how many numbers are after the decimal place in PI, defined as 3.1415926535897

Here is my work:
//Mike Karbowiak 9/19/13
//This program has the user format PI, meaning he or she chooses how many deciaml places PI should be. (i.e. tenths, hundredths, etc.)

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cwchar>
#define PI 3.1415926535897

using namespace std;

int main ()
{
float input;

cout<< "Hello, today you are going to format PI. \n" <<endl;
cout<< "Remember.. PI is 3.1415926535897 ... \n" <<endl;

cout<< "Now that you understand PI to a few numbers where would you like the numbers to end? \n" <<endl;



cin>>input;
cout<<endl;

cout<< "This is PI: " << PI <<endl;

system ("pause");

cout<<endl;

cout<< "This is PI according to you: " << cin.precision(input) << PI << endl;

system ("pause");

return 0;
}


I keep getting the first PI to cut off at .14159 and the next line is always 63.14159 no matter what precision is entered.

How can I fix this?
The example of set precision on here is actually very nearly exactly what your assignment is. Read it, learn it and adapt it to your needs.

http://www.cplusplus.com/reference/iomanip/setprecision/

(and don't use #define PI use double)
Last edited on
Read the documentation found here:

http://www.cplusplus.com/reference/ios/ios_base/precision/

Try this code:
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
/*Mike Karbowiak 9/19/13
This program has the user format PI, meaning he or she
chooses how many deciaml places PI should be.
(i.e. tenths, hundredths, etc.)*/

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cwchar>
#define PI 3.1415926535897

using namespace std;

int main () {

	int input;

	cout<< "Hello, today you are going to format PI. \n" <<endl;
	cout<< "Remember.. PI is 3.1415926535897 ... \n" <<endl;

	cout<< "Now that you understand PI to a few\n"
		<< "numbers where would you like the numbers to end?\n" <<endl;

	cin >> input;
	cout << endl;

	cout.setf(ios::fixed, ios::floatfield);
	cout << "This is PI: " << PI <<endl;

	cout<<endl;
	cout.precision(input);
	cout<< "This is PI according to you: " << PI << endl;

	return 0;
}



Also see this answer on stackoverflow:
http://stackoverflow.com/a/554134/2089675


Using stackoverflow responses:

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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cwchar>
#include <limits>
#define PI 3.1415926535897

using namespace std;

int main () {

	int input;

	cout<< "Hello, today you are going to format PI. \n" <<endl;
	cout<< "Remember... PI is 3.1415926535897... \n" <<endl;

	cout<< "Now that you understand PI to a few\n"
		<< "numbers where would you like the numbers to end?\n" <<endl;

	cin >> input;
	cout.precision(numeric_limits<double>::digits10);

	cout << "This is PI: " <<  PI <<endl;

	cout<<endl;
	cout.precision(input);
	cout<< "This is PI according to you: " << PI << endl;

	return 0;
}
Last edited on
Okay, these really helped. However, my output for line 28 comes out to be

0x1. # bp ^+1 or something like that. Why is this happening? lol


EDIT:
NVM, I found the mistake. Thanks a lot guys!
Last edited on
Topic archived. No new replies allowed.