Visual Studio Problem (C)

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

Dec 14, 2011

Visual Studio Problem (C) by Raiser at 7:47 AM (398 Views / 0 Likes) 5 replies

  1. Raiser
    OP

    Member Raiser I am mad scientist.

    Joined:
    May 31, 2008
    Messages:
    1,565
    Country:
    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

    Banned junkerde Banned

    Joined:
    Jan 3, 2011
    Messages:
    483
    Country:
    United States
    damn tahts some technical looking shit. sorry.
     
    1 person likes this.
  3. Snailface

    Member Snailface My frothing demand for 3ds homebrew is increasing

    Joined:
    Sep 20, 2010
    Messages:
    4,324
    Location:
    Engine Room with Cyan, watching him learn.
    Country:
    Antarctica
    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

    Member Raiser I am mad scientist.

    Joined:
    May 31, 2008
    Messages:
    1,565
    Country:
    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

    Member SanGor Witchhunter

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

    Member Normmatt Former AKAIO Programmer

    Joined:
    Dec 14, 2004
    Messages:
    2,135
    Country:
    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;
    }
    }
    
     

Share This Page