While loop re-initiates itself!

This is a discussion on While loop re-initiates itself! within the C++ Programming forums, part of the General Programming Boards category; Hi All, I've created a while loop which, once the condition is satisfied, stops. But then a few lines later ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    Question While loop re-initiates itself!

    Hi All,

    I've created a while loop which, once the condition is satisfied, stops. But then a few lines later if the condition is "unsatisfied", the while loop decides to reinitiate itself.. I'll show you what I mean:

    Code:
    bool is_line_valid()
    	 {
            //Return true if there is a new line character after the last #
    		list<char>::iterator list_iter = row_chars.end();
    		while(*list_iter != '#')
    		{
    		list_iter--;
    		}
    		cout << "Finished while loop; print what is at current container: " << *list_iter << "\n";
    		cout << "Now move container forward (to last position)... \n";
    		list_iter++;
    		if(*list_iter == '\n')
    		return true;
    		else
    		return false;
    	 }
    And here's the output (perpetual of course):

    Code:
    Finished while loop; print what is at current container: #
    Now move container forward (to last position)...
    Finished while loop; print what is at current container: #
    Now move container forward (to last position)...
    Finished while loop; print what is at current container: #
    Now move container forward (to last position)...
    
    ..etc.
    Can someone tell me why this happens please?
    Last edited by abrownin; 04-30-2010 at 10:35 PM. Reason: Updated with CODE tags

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It doesn't. You're probably calling this function in a loop, I would guess.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    If I remove the line:

    Code:
    list_iter++;
    It actually works perfectly. Yes, I am calling the function in a loop. But the loop does not finish it's first iteration, as the WHILE loop in the function shown will not finish...

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    What I mean is, it never returns true or false, unless I remove the aforementioned line.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by abrownin View Post
    What I mean is, it never returns true or false, unless I remove the aforementioned line.
    Give us complete code.

    EDIT: Or better yet, do something like this:
    Code:
    std::cout << std::boolalpha << is_line_valid() << std::endl;
    and see if it doesn't print true/false every time.
    Last edited by tabstop; 04-30-2010 at 10:43 PM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Thanks for the help! I ended up not needing to use this piece of code, so I won't need to work on it anymore. But I appreciate the suggestions.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    Yeah, the above code you posted is not an infinite loop. You are doing something else wrong. I bet you are calling the function from some infinite loop that you are now going to look at and go... uhh ohh. I bet we will never see you post a reply to this too. Don't feel bad, I have done it too

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Haha don't worry if you miss me I've already posted a new thread with a new completely unrelated problem.

    You know, just to spice this place up a bit.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The above code contains several errors, though, which makes the results undefined:
    Code:
    bool is_line_valid()
    	 {
            //Return true if there is a new line character after the last #
    		list<char>::iterator list_iter = row_chars.end();
    		while(*list_iter != '#') //dereferences row_chars.end() right away; what happens if there is no '#'? 
    		{
    		list_iter--;
    		}
    		cout << "Finished while loop; print what is at current container: " << *list_iter << "\n";
    		cout << "Now move container forward (to last position)... \n";
    		list_iter++;
    		if(*list_iter == '\n') //what if '#' was the last character?
    		return true;
    		else
    		return false;
    	 }
    You could rather make use of the standard algorithms, for example:

    Code:
    bool is_line_valid()
    {
        std::list<char>::const_reverse_iterator it = std::find(row_chars.rbegin(), row_chars.rend(), '#');
        if (it != row_chars.rend()) //found
        {
            std::list<char>::const_iterator next = it.base();
            return next != row_chars.end() && *next == 'n'; //if next character exists and is line-break
        }
        return false;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Wow anon, that's some really great code -- thanks so much for the advice! It's definitely something I wouldn't have thought of in my beginner stage, but it makes a lot of sense.

    Sadly, after re-reading my assignment specification, it turns out I'm not meant to test for if the line is valid (for that test in particular) - as it's assumed that this is so. So I can't use it

    Again, thanks for the response! Helps me notice more things

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 10:46 PM
  2. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 12:54 AM
  3. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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