Need alittle help making this loop

This is a discussion on Need alittle help making this loop within the C++ Programming forums, part of the General Programming Boards category; Hello again, I have decided to start from scratch on the code I mentioned before. I am somewhat close to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23

    Need alittle help making this loop

    Hello again, I have decided to start from scratch on the code I mentioned before. I am somewhat close to my answer I believe, I just can't get the entire thing to loop over again. Could someone look over it and find my error?

    Just to remind everyone, I am trying to find how many terms of a series you need to use before you get the approximation p of Pi, where:
    a) p = 3.0
    b) p = 3.1
    c) p = 3.14
    d) p = 3.141
    e) p = 3.1415
    f) p = 3.14159
    The series is 4 X (1 - 1/3 + 1/5 - 1/7 + 1/9 + 1/11 - 1/13...)

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	double pi= 3.14195;
    	double a = 1, p = 1;
    	double n = 3, t = 0, sign = -1;
    	bool check = true;
    	int h = 0, finish = 0;
    	do
    	{
    
    	while (check)
    	{
    		a += sign*(1.0/(n));
    		sign = -sign;
    		t++;
    		n+= 2;
    		cout << a << " " << t << " " << n << " " << endl;
    		if (a*4 >= pi-(1*pow(10,-p)) && a*4 <= pi + (1*pow(10,-p)))
    			check = false;
    	}
    	cout << a*4 << " " << t << endl;
    	finish++;
    	p++;
    	t = 0;
    	a = 1;
                    check = true;
    	}
    	while (finish < 6);
    	return 0;
    }
    Edit: ROFL! I'm an idiot, i put finish is greater than 6... where as I want it to be less than. I appologize for that waste of time, I make stuipid mistakes with < and > signs all the time.

    2nd Edit: It seems to calculate through the first time but it doesn't seem to calculate it anymore the second time. I made some changes to the code and am replacing the old code with what I have so far.
    Just to be clear, what I am doing is trying to make it estimate how many times it takes till it is equal to 3.0 + or - .1, then displaying the number (which I need to fix as well) and how many times it needed to run the test before it got within that parameter. Then it tests for 3.1 with + or - .01 and displayed that and so forth until it reaches 3.14195

    3rd Edit: Figured out why it wasn't repeated, had to set check back to true. If anyone has any advice to help me refine this code, I am open to it. I am not very experieneced as of yet with coding, so all advice/critisim/whatever is welcome.
    Last edited by Daesom; 10-31-2006 at 12:44 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while (finish > 6);
    At this point finish will be 1, so you probably mean to use the < operator instead of >.
    My best code is written with the delete key.

  3. #3
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	do // this loop is not looping
    if this is supposed to be a question
    then I would change
    Code:
    	while (finish > 6);
    to
    Code:
    	while (finish < 6);
    Kurt
    Edit: bit slow I am

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Pennsylvania
    Posts
    23
    Thanks for the correction, as I said before, I get those two signs messed up a lot when I do coding, heh...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. For loop takes time
    By Shakti in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2004, 08:25 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21