Need help with a strange for-loop problem

This is a discussion on Need help with a strange for-loop problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a really really strange problem with a for loop. Let me show you my code first: Code: ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    37

    Question Need help with a strange for-loop problem

    Hi,

    I have a really really strange problem with a for loop. Let me show you my code first:

    Code:
    if(size == 2){
       MessageBox(hwnd, "2", "size", MB_OK);
    }
    for(long c = 0; (c < size) && (flag); c++){
       //loop body
    }
    if(!flag){
       //there is a prompt
    }
    The strange thing here, is that my size is really 2, as the MessageBox will appear, but the for loop only runs one time, and the flag == false prompt does not show (i.e. the for loop isn't stopped by the flag). Even more strangely, if I hard code (c < 2) instead of (c < size), then it works very fine.

    Anyone knows what's the problem? By the way I'm using Dev-C++ 4.

    Thanks in advance.

    EDIT: Does it help if I tell that my whole project consists of >1300 lines? Does it matter?
    Last edited by jasperleeabc; 05-29-2009 at 03:24 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Try showing a small but complete sample of code that illustrates your problem.

    My guess is that your actual code is doing something different from the code you've posted here.

    One of the common joys of paraphrasing (i.e. telling us where you think the problem is, rather than posting real code that exhibits your problem) is that your description often excludes the cause of your actual problem.

    It shouldn't matter that your project consists of > 1300 lines - although that is not an invitation to post that code here. Do some work to produce the small but complete example that illustrates your problem.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should at least show the loop. Best guess with no information whatsoever: your for-loop (re)sets flag inside it somewhere.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    It sounds like you're changing size inside your loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    37
    Somehow I forgot to look for if I have changed "size" in the loop, I only looked for any change in "c" and "flag". I'll check it later (since it's late at night in local time already). Thanks for you guys' help anyway.

    Many thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 09:35 PM

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