### 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

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

Try this code:
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````/*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 #include #include #include #include #define PI 3.1415926535897 using namespace std; int main () { int input; cout<< "Hello, today you are going to format PI. \n" <> input; cout << endl; cout.setf(ios::fixed, ios::floatfield); cout << "This is PI: " << PI <

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

Using stackoverflow responses:

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include #include #include #include #define PI 3.1415926535897 using namespace std; int main () { int input; cout<< "Hello, today you are going to format PI. \n" <> input; cout.precision(numeric_limits::digits10); cout << "This is PI: " << PI <
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.