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.