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
  • No one is chatting at the moment.
  • K3Nv2 @ K3Nv2:
    I'll reformat and have a 3tb raid0 m. 2 at least
    +1
  • K3Nv2 @ K3Nv2:
    Lmao that sold out fast
    +1
  • Veho @ Veho:
    Yeet the cat.
    +1
  • K3Nv2 @ K3Nv2:
    Good idea
    +1
  • The Real Jdbye @ The Real Jdbye:
    i thought everybody knew cocktails are like 75% ice
  • Veho @ Veho:
    Yeah but not like this.
  • Veho @ Veho:
    It's not like they're complaining that their Slurpee is 99% ice or something, but if the cocktail calls for "shot of vodka, shot of vermouth, shot of gin, shot of Campari, three shots of juice, squirt of lemon" and ends up being a thimbleful of booze, that's a problem.
  • The Real Jdbye @ The Real Jdbye:
    the funny thing is cocktails in norway are only allowed to have 1 20ml shot of booze
  • The Real Jdbye @ The Real Jdbye:
    so..... yeah
  • The Real Jdbye @ The Real Jdbye:
    we're used to only having a thimbleful of booze
  • Veho @ Veho:
    Booo.
  • The Real Jdbye @ The Real Jdbye:
    same thing if you want whisky on the rocks or something, you can't get a double
  • The Real Jdbye @ The Real Jdbye:
    but you could buy as many shots of whisky (or anything else) as you want and ask for a glass of ice and pour them in
  • The Real Jdbye @ The Real Jdbye:
    it's dumb
  • Veho @ Veho:
    Maybe.
  • Veho @ Veho:
    There was a comparison of the number of Ibuprofen poisonings before and after they limited the maximum dosage per box or per pill (i'll look that up). No limit on the number of boxes you can still buy as many as you want, so people argued it was pointless.
  • Veho @ Veho:
    But the number of (accidental) poisonings dropped because drinking an entire package of ibuprofen pills went from "I need a new liver" to "I need a new box of Ibuprofen".
  • Veho @ Veho:
    Here we have ketoprofen that used to be prescription-only because of the risk of toxic dosages, but then they halved the dose per pill and sell them in bottles of six pills apiece instead of twenty and it doesn't need a prescription any more. Yes you can buy more than one bottle but people simply don't.
  • Psionic Roshambo @ Psionic Roshambo:
    Usually accidentally overdose of ibuprofen here is from people taking like cold medicine then ibuprofen for a headache and the combination is over what they need
    Veho @ Veho: https://imgur.com/gallery/QQkYnQu