new to all this, please don't shoot the messenger. using an input file to estimate pi but the output is wrong. I know it's obvious but I can't see it. Is it a math problem or object problem?
[code] int main() {
ifstream infile("lab05.txt");
ofstream outfile("out.txt");
double pi, sum, num = -1.0;
long count;
bool done=false;
while(!infile==done) {
while (infile >> count) {
for (long denom = 1; denom < count; denom = denom + 2) {
num *= -1.0;
sum += (num / denom);
}
pi =4*sum;
cout << "Number of iterations use to est. pi:\t" << count << " ""Pi is roughly:"" " << pi << endl;
}
infile.close();
outfile.close();
}
return 0;
:\Users\Skittles\CLionProjects\kitchenbradLab05\cmake-build-debug\kitchenbradLab05.exe
Number of iterations use to est. pi: 12 Pi is roughly: 2.97605
Number of iterations use to est. pi: 123 Pi is roughly: 6.13403
Number of iterations use to est. pi: 1234 Pi is roughly: 2.99082
Number of iterations use to est. pi: 12345 Pi is roughly: 6.13225
Number of iterations use to est. pi: 123456 Pi is roughly: 9.27382
Number of iterations use to est. pi: 1234567 Pi is roughly: 12.4154
Number of iterations use to est. pi: 12345678 Pi is roughly: 9.27383
Number of iterations use to est. pi: 123456789 Pi is roughly: 12.4154
#include <iostream>
#include <fstream>
int main()
{
std::ifstream infile("lab05.txt");
std::ofstream outfile("out.txt");
double pi, sum, num = -1.0;
long count;
bool done = false;
while (!infile == done)
{
while (infile >> count)
{
for (long denom = 1; denom < count; denom = denom + 2)
{
num *= -1.0;
sum += (num / denom);
}
pi = 4 * sum;
std::cout << "Number of iterations use to est. pi:\t" << count << " ""Pi is roughly:"" " << pi << std::endl;
}
infile.close();
outfile.close();
}
return 0;
}
A few blank lines makes the code much easier to read.
Line 6 refers to an input file. Where is it and what does it look like?
Lines 20 and 21 use "*=" and "+=", so why did you not use it at the end of tine 18?
In your first while loop while (!infile) is all you need. Actually this outer while loop should be an if statement to check if the file stream open and is usable.
1 2 3 4 5 6
if (!infile)
{
std::cout << "\n file did not open!\n";
return 1;
}
Your OP shows some output, but is that what you want or is it what the program is doing?
With out the input file it is hard to test the program.
It's (primarily) a maths problem, although there are computational niceties that could be improved.
Read @Ganado's post, and note that sum has to be set back to 0 immediately after reading count EVERY TIME. The same is true of your sign variable, num. Thus:
1 2 3 4
while (infile >> count) {
sum = 0.0; // <===== Each summation has to start from scratch
num = -1.0; // <===== Ditto
for (long denom = 1; denom < count; denom = denom + 2) {
Although you won't notice it easily if you have a lot of terms in your series, you are currently looping up to a maximum value of the denominator, and NOT the number of terms. That needs to be fixed, either by changing the final denominator, or rewriting the denominator in terms of a loop-counting variable.