Visual Studio Problem (C)

Discussion in 'Computer Games and General Discussion' started by Raiser, Dec 14, 2011.

  1. Raiser
    OP

    Raiser I am mad scientist.

    Member
    1,565
    40
    May 31, 2008
    Canada
    I need some help with an issue I'm having.
    I'm currently coding something via Visual Studio C++ 2010 Express where I have something similar to:

    Code:
    // headers included
    // bunch of declarations and initializations
    int close = 0;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    // code unrelated to 'close'
    do
    {
    int menuInput = _getch();
    switch(menuInput)
    {
    // bunch of code also unrelated to 'close'
    case 'x':
    close = 1;
    break;
    }while(close == 0);
    return 0;
    }
    Now for my problem... I will run the program, but it will eventually unexpectedly end (Press any key to continue...).
    At first I was completely puzzled as 'close' should have been 0 the entire time. I stuck a:

    Code:
    printf("%d\n",close);
    directly above

    Code:
    return 0;
    And I find that 'close' ends up being anywhere between 20-23 which is the exact same as another variable of a completely different name!
    Using CTRL+F "close", the only results are whatever you see above. It is not used or changed in any way in the "bunch of code".

    I'm not necessarily looking for a direct fix (I want to avoid pasting all of my code), but does anyone know a potential cause / explanation for this?

    Thanks.
     
  2. junkerde

    junkerde Banned

    Banned
    483
    37
    Jan 3, 2011
    United States
    damn tahts some technical looking shit. sorry.
     
    1 person likes this.
  3. Snailface

    Snailface My frothing demand for 3ds homebrew is increasing

    Member
    4,324
    1,983
    Sep 20, 2010
    Engine Room with Cyan, watching him learn.
    I can't figure out what is causing the problem with such a small snippet of code but replacing
    }while(close == 0);
    with
    }while(close != 1);

    may do the trick.

    @OP y/welcome :P

    (I'm wondering if he has a buffer overflow somewhere. oh well that little patch should be good enough)
     
  4. Raiser
    OP

    Raiser I am mad scientist.

    Member
    1,565
    40
    May 31, 2008
    Canada
    Thanks, Snailface! This did the trick (hopefully). I can't believe I didn't think of such an obvious change. I still am wondering how 'close' even changed...

    I realize it's not much to look at, but I have double and triple-checked- 'close' is not manipulated in any way in the entire program except for what I mentioned.

    Thanks again!
     
  5. SanGor

    SanGor Witchhunter

    Member
    993
    79
    Aug 21, 2008
    United States
    You should really post the whole code and not such mess where half the code is missing.
     
  6. Normmatt

    Normmatt Former AKAIO Programmer

    Member
    2,142
    544
    Dec 14, 2004
    New Zealand
    You should change your code to work on the principal of run until this variable is not true

    Code:
    bool running = true;
    while(running)
    {
    int menuInput = _getch();
    switch(menuInput)
    {
    // bunch of code also unrelated to 'close'
    case 'x':
    running = false;
    break;
    }
    }