Help!!

I am writing my first C++ program and it stops after asking for the input. Here is what I was asked to do:

Write a program that prompts a user for the desired accuracy for a calculation of π and then applies the sum given above to find an approximate value of π within that accuracy.

You will want to use a double to hold your estimate as you accumulate it. Because doubles have a finite number of digits, we will need to limit the accuracy that we shoot for. For this assignment, we will never try to compute π to an accuracy of more than ±0.0001.

To compute the sum, you will need to use a while loop. The loop should keep track of how many terms have been added into the sum so far. It should compute the denominator (d) and then the next the next term (4.0/d). The loop should exit when 4.0/d is less than the desired accuracy. (You should still add or subtract the final 4.0/d from your sum.)
Input to the program will be taken from standard input (cin). Output will be written to standard output (cout).

Your program should prompt the user with the phrase

How accurate an estimate should we attempt?
with a blank space (but no line feed) after the question mark. It should read in the answer (as a double).

If the number entered is less than 0.0001, you should print the line

That number is too small. Using 0.0001 instead.
and then replace the desired accuracy by that value.

Your program should then proceed to compute its estimate of Pi as described above. Each time that you add in another term, your should print a line containing the number of terms used so far, a colon (:), a space, then the sum accumulated so far. Here is my code:

[code]

//Compute an estimate for pi using Leibniz`s formula
#include <iostream>
#include <cmath>


using namespace std;

int main ()
{
double pi;
double accuracy;
double limit = 0.0001;

// ask user for accuracy
cout << "How accurate an estimate should we attempt? " << flush;
cin >> accuracy;
// if number is too small use 0.0001
while(pi < accuracy)
{
if (accuracy > limit)
cout << " That number is too small. Using 0.0001 instead." << flush;
accuracy = limit;
int accuracy = 0;
while(accuracy>6)// calculate pi
{
pi=-4/(2*accuracy+1);
accuracy++;
}
}
cout << pi << endl;//output results
pi++

return 0;
}

Last edited on

What is the value of pi that you are checking against? - its not initialised.
pi is not initialized?
closed account (48T7M4Gy)
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
#include <iostream>
#include <cmath>


using namespace std;

int main ()
{
double pi= 3; //<---
double accuracy;
double limit = 0.0001;

// ask user for accuracy
cout << "How accurate an estimate should we attempt? "; //<---
cin >> accuracy;
// if number is too small use 0.0001
while(pi < accuracy)
{
if (accuracy > limit)
cout << " That number is too small. Using 0.0001 instead." ; // <---
accuracy = limit;
int accuracy = 0;
while(accuracy>6)// calculate pi
{
pi=-4/(2*accuracy+1);
accuracy++;
}
}
cout << pi << endl;//output results
pi++; // <----

return 0;
}


That's a start but there's something wrong with the iteration. I'd suggest you re-write the instructions you are given as psedocode with one instruction on one line. In other words deconstruct the paragraphs you are given.

Also when you get that straight and modify your code use proper indentation. :)
Last edited on
Topic archived. No new replies allowed.