Trouble with player health (Basics)

Hi I'm testing out what I can do with C++ and I want to eventually develop a simple text based game. So I'm trying to create my own health system. The problem in the coding is that once my health goes to 0 or negative it will not go to a death scene and that I added it'll just loop the program.

Any other suggestion in how to do this would be appreciated,
Thank you


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>


using namespace std;


int main()
{
	int hp = 9; 
	int enemyattack = 2

	
	;get_attacked:
	;system("cls");
	cout << "You have 9 health, everytime you press a button it drops by two";
	_getch();
	hp = hp - enemyattack;
	cout << "You now have " << hp << "HP left.\n\n";
	_getch();
		if ('hp' <= 0 ) {
	goto dead;
	}
	else if ('hp' > 0) {
	goto get_attacked;
}
	_getch();


dead:
cout << "You have no more health, you dead";
_getch();
line 20

if ('hp' <= 0)

if you get rid of '' (single quotes) your code will work as desired as hp is a variable and not a character

PS: you are using too many goto: coders tend to get angry whenever someone uses goto :)
uh..dude, where is dead ?

oh and using system to clear the screen is bad...
visit this article
http://www.cplusplus.com/articles/4z18T05o/
Last edited on
i think this is a better code....???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

void clrscr()
{
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
}

int main()
{
    int health = 10;
    int EnemyAttack = 2;
    cout << "you have 10 health, each time you pressed a button, your health decreased by 2";
    getch();
    do
    {
        clrscr();
        cout << "you have " << health << " health left";
        getch();
        health -= EnemyAttack;
    } while (health > 0);
    clrscr();
    cout << "you are dead";
}


anyways, what compiler / ide are you using ?
coders tend to get angry whenever someone uses goto

Not angry, I just cry tears of blood. And only when misused. (It is being misused now...) :'(

As a beginner I would recommend that you don't use it. The circumstances in which its use is warranted are relatively rare, and the potential for misuse is high.

goto is occasionally useful for various low-level micro optimizations, and for multi-level breaks in certain very limited situations. Using it because you're too lazy to re-structure your code leads quickly to a fragile and generally unmaintainable pile of overcooked spaghetti. Try not to do that.
https://en.wikipedia.org/wiki/Spaghetti_code

Dijkstra said it best:
http://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf

P.S.: If the only control-structure you're aware of is goto and if it's essentially impossible to not use goto. The solution is to get a basic understanding of the language, first.
Last edited on
mbozzi wrote:

I just cry tears of blood


can I have your blood ?
Last edited on
Ok thanks guys, I got it working (thx shadder). Btw Flaze07 I'm using Dev C++, just cause my computer is really shit so I need something that won't slow it down. P.S I used goto and cls a lot cause I'm more familiar with cmd.
Last edited on
Topic archived. No new replies allowed.