Gaming Visual Studio Problem (C)

Raiser

I am mad scientist.
OP
Member
Joined
May 31, 2008
Messages
1,563
Trophies
0
Website
Visit site
XP
636
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.
 

Snailface

My frothing demand for 3ds homebrew is increasing
Member
Joined
Sep 20, 2010
Messages
4,324
Trophies
2
Age
40
Location
Engine Room with Cyan, watching him learn.
XP
2,256
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)
 

Raiser

I am mad scientist.
OP
Member
Joined
May 31, 2008
Messages
1,563
Trophies
0
Website
Visit site
XP
636
Country
Canada
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.
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!
 

Normmatt

Former AKAIO Programmer
Member
Joined
Dec 14, 2004
Messages
2,161
Trophies
1
Age
33
Website
normmatt.com
XP
2,189
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;
}
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    SylverReZ @ SylverReZ: @OctoAori20, Cool. Same here.