Simple question about # of digits.

I'm modifying a project that I'm working on to give the user more control.
One of the parts of my project involves using pi (3.1415926535 etc).
I would like to be able to set pi equal to a huge number, then give the user the opportunity to express how many decimals they would like pi used to.

Below is a dumbed down version of my project involving just the pi code.

If anyone could help me figure out how to do this, I'd be grateful!


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
#include "stdafx.h"
#include <iostream>
using namespace std; 
double piChoice(double);
double piEditor(double);

int main(){
   double pi = 3.14159;
   
   pi = piChoice(pi); 
   piEditor(pi);

return 0;
}

double piChoice(double pi){
	int decimal;

	cout << "Pi can be calculated from 2 to 5 decimal places." << endl;
	cout << "How many decimal places would you like it calculated to: "<< endl; 
	cin  >> decimal;
    while (decimal < 2 || decimal > 5){
	   cout << "Please chose a number between 2 and 5." << endl; 
	   cout << "How many decimal places would you like pi calculated to: " << endl; 
	   cin  >> decimal;
	}

    // Some sort of code to set pi to extend out "decimal" number of digits.  

	return pi;
} 

double piEditor(double pi){

// This is where I will be using the modified pi. 
// For simplicity's sake, I'll just stick an output here
// instead of using the 6 page code I have.

   cout << "Your pi equals: " << pi << endl;

   return 0;
}
closed account (3TXyhbRD)
Implement a fraction number (retains counter and denominator). When returning the number as float use an array of ints so you don't need to worry about float or double type size restrictions.
I don't think fractions are really needed in this.

I figured out one way to accomplish my goal, but it's kind of... long winded...


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std; 
int piChoice();
int piEditor(int);

int main(){
   double pi = 0;
   
   int dec = piChoice(); 
   piEditor(dec);

return 0;
}

int piChoice(){
	int decimal;

	cout << "Pi can be calculated from 2 to 14 decimal places." << endl;
	cout << "How many decimal places would you like it calculated to: "<< endl; 
	cin  >> decimal;
    while (decimal < 2 || decimal > 14){
	   cout << "Please chose a number between 2 and 14." << endl; 
	   cout << "How many decimal places would you like pi calculated to: " << endl; 
	   cin  >> decimal;
	}

	return decimal;
} 

int piEditor(int dec){
    double pi;

        if      (dec ==  1)
            pi = 3.14;
	else if (dec ==  3)
            pi = 3.141;
	else if (dec ==  4)
	    pi = 3.1415;
	else if (dec ==  5)
		pi = 3.14159;
	else if (dec ==  6)
		pi = 3.141592;
	else if (dec ==  7)
		pi = 3.1415926;
	else if (dec ==  8)
		pi = 3.14159265;
	else if (dec ==  9)
		pi = 3.141592653;
	else if (dec == 10)
		pi = 3.1415926535;
	else if (dec == 11)
		pi = 3.14159265358;
	else if (dec == 12)
		pi = 3.141592653589;
	else if (dec == 13)
		pi = 3.1415926535897;
	else if (dec == 14)
		pi = 3.14159265358979;

   cout << setprecision(15) << pi << endl;

   cout << "Your pi equals: " << pi << " with " << dec << " decimals. " << endl;

   return 0;
}
Any other ideas to shorten this?
1
2
3
4
5
6
7
8
9
10
int piEditor(int dec){
    double pi = 3.14159265358979 ;

	if ( dec <= 1 )
		dec = 2 ;

   cout << "Your pi equals: " << setprecision(dec+1) << pi << " with " << dec << " decimals. " << endl;

   return 0;
}


You don't round correctly in your function. piEditor seems like a very inaccurate name -- you aren't editing anything. So does the wording of the prompt since you aren't actually calculating pi. I also wonder why piEditor return an int.
Last edited on
Because this is just a dummy program representing a single problem in a 6 page project I'm working on.
Topic archived. No new replies allowed.