Thread: Why won't this loop terminate

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Why won't this loop terminate

    The following loop will never terminate and I can't for the life of me work out why not.

    birthmonth=7 / thismonth=10

    I put the cout<<y; to try to see but it just continually prints '9'.

    Code:
     if (birthmonth < thismonth)
            {
                    for(int y=(birthmonth + 1);y = (thismonth-1);y++)
                    {
                           cout<<y;
                            daysold = daysold + monthdays[y];
                    }
            }
    Please help I am losing all my hair!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > y = (thismonth-1)
    Mixing up assignment and comparison (again).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    it is so obvious when explained - sorry for wasting your time.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Both VC++ and GCC will (usually) generate warnings for this. What compiler are you using?
    Also, you should consider learning to use a debugger. They are essential in programming and easily allows you to pick up mistakes such as this!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I am using Code::Blocks; and yes I would like to learn to use the debugger supplied with it - can you recommend any books on the subject as I have looked around and there doesn't appear to be much info on actually using them. Up to now I get by by putting thinks like cout<<y; to watch what is happening and in this case I could see the loop was just continual but couldn't see why - having used = as an assigner and a comparison for most of my programming life it is hard to remember that C++ is different though I think it may have sunk in now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions that should terminate. Really.
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-26-2007, 10:56 AM
  2. terminate loop
    By miryellis in forum C Programming
    Replies: 3
    Last Post: 09-07-2004, 07:14 AM
  3. how to terminate for loop
    By Chobo_C++ in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2004, 05:43 PM
  4. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM
  5. How do I terminate a FOR Loop?
    By JYoung in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-14-2001, 03:37 AM