Using an array to manipulate Pi to specific decimal Place

I have a class assignment where I have to take pi as a constant and set it to a user desired decimal place. The last digit cannot be rounded so the use of setprecision or printf wont work. I set my constant pi as an array and then try to index it to the user specified decimal place. I need to out put the final pi number and then use for a calculation later on. I want to know where I am going wrong on indexing the array and how to get it to display as a floating or double decimal number.

# include <iostream>
# include <cmath>
# include <iomanip>

using namespace std;
const char pi_arr[]= {3,'.',1,4,1,5,9,2,6,5,3,5,8,9,7,9,3};
int user_input;
double pi_output;
int main ()
{


cout << "Please Enter Number of Decimal Places for Pi from 0 to 15" <<endl;
cin >> user_input;

if (user_input >= 0 && user_input <= 15)
{for (int i = 0;i < user_input+2; i++)
{
pi_output = pi_arr[i];
cout <<" Your Pi is: "<< pi_output << endl;
}


}
else
{ cout << " Invalid Selection, Please Rerun Program and Select A Number Only Between 0 and 15!!!"<< endl;
}

return 0;
}
Why not simply
 
std::cout << " Your Pi is: " << "3.141592653589793"s.substr(0, user_input + 2) << std::endl;
?
If I try to run it like that it says s is not declared along with other syntax errors.
Must be an old compiler.
1
2
3
std::cout << " Your Pi is: " << ((std::string)"3.141592653589793").substr(0, user_input + 2) << std::endl;

	
The reason s doesn't work is that you have to do using std::string_literals::operator""s;

If that doesn't work, then it is probably an outdated compiler.
Last edited on
That worked to get the right output, Thank you.

The next issue is I have to use that output for a calculations, but I get error message saying
[Error] no match for 'operator*' (operand types are 'double' and 'std::string {aka std::basic_string<char>}')

const double pi = 3.141592653589793;
double r = 5.0;
double A1 = r*r*pi;
double A2 = r*r*pi_output; // error message is on this line when compiled//
double diff = A1 - A2;

cout << diff << endl;
I don't think I understand the assignment. Could you post the problem statement verbatim?
Write a program that generates PI to a user defined decimal places( no rounding). PI is estimated at 3.141592653589793.

1. The user will be prompted to enter a number of decimal places between 0 and 15, if the user enters anything else the program will exit displaying invalid message. [ this step was done with the if statement at the beginning.]

2. If the input is valid, the program will display the pi(pi_output) to the exact decimal places the user entered with no rounding.

[- You helped me with this by creating a string and then i set pi_output to that.
pi_output = ((string)"3.141592653589793").substr(0, user_input + 2);

cout << " Your Pi is: " << pi_output << endl;
- everything works right up to here.]


3. Assuming radius is 5.0, calculate the area of a circle using the user truncated pi(pi_output).

-Now i need to be able to multiply (pi_output*r*r) to get the area but pi_output is string and wont let me multiply.
- r was declared as a double.
It sounds like you're expected to do floor(pi * 10^input) * 10^-input.
Topic archived. No new replies allowed.