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!
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
}
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.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
usingnamespace 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;
elseif (dec == 3)
pi = 3.141;
elseif (dec == 4)
pi = 3.1415;
elseif (dec == 5)
pi = 3.14159;
elseif (dec == 6)
pi = 3.141592;
elseif (dec == 7)
pi = 3.1415926;
elseif (dec == 8)
pi = 3.14159265;
elseif (dec == 9)
pi = 3.141592653;
elseif (dec == 10)
pi = 3.1415926535;
elseif (dec == 11)
pi = 3.14159265358;
elseif (dec == 12)
pi = 3.141592653589;
elseif (dec == 13)
pi = 3.1415926535897;
elseif (dec == 14)
pi = 3.14159265358979;
cout << setprecision(15) << pi << endl;
cout << "Your pi equals: " << pi << " with " << dec << " decimals. " << endl;
return 0;
}
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.