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,199
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
  • K3Nv2 @ K3Nv2:
    Sorry I sat on them when they were open
  • AncientBoi @ AncientBoi:
    eeewww
  • K3Nv2 @ K3Nv2:
    I thought it was the toilet
  • AncientBoi @ AncientBoi:
    okies. Time to go watch YT paranormal ghost things. L8er my luvs :D
    +1
  • K3Nv2 @ K3Nv2:
    I got a massive clue
  • BakerMan @ BakerMan:
    this mf def ain't watching ghost shit, he boutta beat his meat fr
    +1
  • K3Nv2 @ K3Nv2:
    Nah he's about to be the ghost in your bedroom
    +1
  • Xdqwerty @ Xdqwerty:
    @K3Nv2, and leave ectoplasm all over the place
  • BakerMan @ BakerMan:

    this is him being described
    +2
  • Xdqwerty @ Xdqwerty:
    Sigh
  • Xdqwerty @ Xdqwerty:
    Yawn
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, I dislike the kind of drm where you have to play single player games online all the time bc of some verification bs
    +1
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Don't use games that have Easy Anti-Cheat as its been exploited many times.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, my PC can't run most AAA games so i wont
    +1
  • Xdqwerty @ Xdqwerty:
    Most of the modern AAA games
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, I also heard one of the Prince of Persia games was so unfinished that it required the "24/7 online" drm so a puzzle could be done and the game could be finished. And that when the Ubisoft servers were closed the (cracked) game was impossible to finish or something like that
  • SylverReZ @ SylverReZ:
    @Xdqwerty, That's extra scummy. Ubisoft nowadays ship out incomplete games like Skull and Bones which was being worked on for nearly a decade now.
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, i think they have been doing that since late 2000s
    +1
  • Xdqwerty @ Xdqwerty:
    Either that or their old games were unfinished aswell but we can't notice it
  • Psionic Roshambo @ Psionic Roshambo:
    I like that games can be fixed after the fact, hate that it's being abused via beta tests... And DLC... I was a 7800 owner back in the day and loved Impossible Mission, turns out I couldn't beat it because it was actually impossible lol
  • Psionic Roshambo @ Psionic Roshambo:
    I never knew about it at the time but a fixed version was available but you had to mail in your broken copy lol
  • Psionic Roshambo @ Psionic Roshambo:
    So that version is semi rare
    Psionic Roshambo @ Psionic Roshambo: So that version is semi rare