avoiding negative integers

i have the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
kit://-----------------------------------------------------------------------kit starts here
{
int a,h=200;
cin>>a;
cout<<endl;
h=h-a;                        


											  

if(hp>=0)
{
cout<<"\n              "<<hp<<"\\100";
}
else
{
cout<<"\n              0\\100\n\n";
cout<<"  dead";
}
}           //------------------------------------------------------------------kit ends here
goto kit;

now the real problem is that when someone dies(in the game of course :D) his/her/its "health point" is simply shown 0 and not a negative integer(sic).... also using the goto fuction i wont be able to proceed ahead in my program as the code will always force me to stay in kit......... what to do????

thanks in advance.......
now the real problem is that when someone dies(in the game of course :D) his/her/its "health point" is simply shown 0 and not a negative integer(sic)

Well, yes. That's because your code checks the value of hp, and does different things depending on whether it's above 0 or not.

What do you want it to do?

also using the goto fuction i wont be able to proceed ahead in my program as the code will always force me to stay in kit


Don't use goto. Seriously. Unless you are very, very experienced in C++, and understand completely the implications of using it, and what the dangers are, and how to avoid those dangers, then it is very unwise to use it.

C++ provides many sensible control structures that are much better for a beginner to use that goto.
can you please suggest what alternative are better and how????
May be you should write line 11 as if (hp > 0)
can you please suggest what alternative are better and how????

In most cases pretty much any alternative is better than goto. use a loop.
http://www.cplusplus.com/doc/tutorial/control/

edit: also (though I'm not sure of your logic) I'd suggest better names for variables and intitialise them. e.g.

1
2
3
4
5
int armour = 0;
int health = 200;
cin>>armour;
cout<<endl;
health = health-armour;   (// i.e. health -= armour;)   

Obviously I don't know what your variables mean (which highlights the issue right there), but that kinda thing.
Last edited on
@mutexe

u guessed all my variables right.... BRAVO...........

i didn't knew that we could use a while with if
Registered users can post here. Sign in or register to post.