Incorrect math or data types

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?

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
34
35
[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
Last edited on
First question I have is: What is the initial value of sum?
Hello bigskit13,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


It looks like you started to, but forgot or messed up the closing tag.

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
34
#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.

Andy
handy andy, yeah I screwed up the formatting. my bad!
It's late and my brain is mush. thanks for the guidance!

Input file: lab05.txt

12
123
1234
12345
123456
1234567
12345678
123456789

each line series should get closer and closer to 3.14159. that's the output I want
Last edited on
bigskit13 wrote:
Is it a math problem or object problem?

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.

You don't need the outer while loop. Remove it.
Last edited on
last chance that worked perfectly. thank you. learned a lot
Topic archived. No new replies allowed.