Correctly use input values

I'm trying to create a program that uses an input file to approximate pi.
Input file has the following in it

12
123
1234
12345
...
123456789

pi formula is this pi=4*(1/1-1/3+1/5..1/9). The idea is for the first iteration to use 1 and 2 as the denom, 2nd 123 and so on from the input file. I'm practicing using for/while loops and input files. I'm so new that I have no idea how to have the compiler use the input file values. How do I create a for loop that uses the input values, calculates pi, then outputs it to a file? i'm lost please help!

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
int main() {

    ifstream infile("lab05in.txt");
    ofstream outfile("out.txt");
    double pi, calc, n=1.0;
    int count;
    char choice;
    bool done = false;
    long denom=(2*n-1);
    cout<<"Would you like to approximate pi?";
    cin>>choice;
        if (choice=='Y' ||choice== 'y') {
            while (!done) {
                while (infile >> count) {
                    cout << "Numerators used to find pi:"" "<< count << endl;
                    for (int i=count; i <= count; i++) {
                        calc = (pow(-1, n + 1) / denom);
                        pi= (4*calc);
                        cout << "Estimated pi value is:"" " << pi;
                        outfile <<count<<" "<<"="" "<<pi;
                    }
                cout<<"\n";

                }
                infile.close();
                outfile.close();

            }
        }
        else {

            cout<<"good bye";
        }
bigskit13 wrote:
uses an input file to approximate pi

Stop right there - the usage of any input file is (probably) to tell you how many terms in the series to use. You can approximate pi perfectly well without an input file. You need to phrase your title and question more carefully. Are you absolutely sure that your assignment requires you to use an input file? You can do successive approximations to pi perfectly well without.


bigskit13 wrote:
The idea is for the first iteration to use 1 and 2 as the denom, 2nd 123 and so on from the input file.

No. Enough said. I really don't think you understand your assignment.




How do I create a for loop that ... uses the input values, ... calculates pi, ... then outputs it to a file?

That's three separate things. Start by doing each task separately. Then life gets much easier. Personally, I would do item 2 below first (partly because I'm not entirely convinced that you need item 1).


(1) Read a file.
Read the tutorials here:
Start with (non-file) input and output
http://www.cplusplus.com/doc/tutorial/basic_io/
and then change to file input and output:
http://www.cplusplus.com/doc/tutorial/files/


(2) Loop to calculate pi with N terms
First set variable calc (or whichever variable you are using to compute the sum) either to 0 or to the first term (your choice). Then loop to get the remaining terms in the series:
1
2
3
4
for ( /* set the number of terms you want*/ )
{
    // add the next term to calc
}

You can find information on loops in
http://www.cplusplus.com/doc/tutorial/control/#loops


(3) Writing files is suspiciously like reading files in reverse. See item (1).



BTW, writing pow(-1, n + 1) is a truly appalling way to get a series with alternating signs. Just keep a variable sign, initialised as (e.g.) int sign=1; and use sign=-sign; whenever you want to flip signs.



BTW2. Writing things like
1
2
3
cout<<"Would you like to approximate pi?";
    cin>>choice;
        if (choice=='Y' ||choice== 'y') {

is a bit pointless. Why else would you be running this program? All you have done is add the complexity of another control structure.
Last edited on
lastchance,
you are correct. i didn't understand the assignment well. in fact most the class didn't because it wasn't written very well. the sequence numbers are the number of iterations to run the calculation of pi. I'll look at the tutorials and try to figure it out. thanks for the pointers
Topic archived. No new replies allowed.