Writing a simple game, having issues with objects

So I'm trying to make a simple turn based game in terminal. I have a header file, and two cpp files. The idea is to make the game highly expandable as a learning tool.
Anyways, when I printout player.hp, I get 1, when I was expecting 500. The eventual goal is to start player.hp at 500, and troll.hp at 500, and run attacK::number to calculate damage every turn.

here's characters.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef RESOURCES_H
#define RESOURCES_H

class Die{
public:
	static int roll();
};

class character{
public:
	static int hp();
	bool state();
};

class attack{
public:
	int number();
};

#endif 

characters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "characters.h"

int damage;

int Die::roll()
{
	return rand()%4+1;
}

int character::hp()
{
	int initialhealth=500;
	int currenthealth = initialhealth-damage;
	return currenthealth;
}

int attack::number()
{
    return damage = Die::roll() * 25;
}

and main.cpp
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
#include <iostream>
#include "characters.h"

int main()
{
	char select;
	character player;
	character troll;
	std::cout << "Hello, welcome to wizardworld version 1" << std::endl;
	std::cout << "there is a troll in the room you are in, engage in combat?" << std::endl;
	std::cout << "enter a to begin combat" << std::endl;
	std::cin >> select;
	if(select=='a')
	std::cout << "You have entered combat" << std::endl;
	else return 0;
	
	std::cout << "Both your hp, and the troll's hp start at 500, you will take turns attacking each other until one of you runs out" << std::endl;
	std::cout << player.hp << std::endl;
		
	srand(time(0));
	Die d;
	int result = d.roll();
	std::cout << result << std::endl;
	return 0;
}

in main.cpp, when I cout player.hp I get 1, and I don't get it. I'm new to programming, and this object oriented stuff is hard.
std::cout << player.hp
This is not how to call the hp function.

This is:
std::cout << player.hp()
That fixed it, thanks.
But, I still don't get why it gave a 1 when I did it wrong.
Alright, new problem, I'm getting "else without a previous if after my if statement.
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
#include <iostream>
#include "characters.h"

int main()
{
	char select;
	character player;
	character troll;
	std::cout << "Hello, welcome to wizardworld version 1" << std::endl;
	std::cout << "there is a troll in the room you are in, engage in combat?" << std::endl;
	std::cout << "enter a to begin combat" << std::endl;
	std::cin >> select;
	if(select=='a')
	std::cout << "You have entered combat" << std::endl;
	else return 0;
	
	std::cout << "Both your hp, and the troll's hp start at 500, you will take turns attacking each other until one of you runs out" << std::endl;
	std::cout << "the players hp is" << player.hp() << std::endl;
	std::cout << "the troll's hp is" << troll.hp() << std::endl;
	player.hp(); 
	attack theplayer;
	attack thetroll;
	
	std::cout << "to attack enter a" << std::endl;
	std::cin >> select;
	if(select=='a')
	theplayer.number();
	std::cout << player.hp() << std::endl;
	else return 0;
	
	srand(time(0));
	Die d;
	int result = d.roll();
	std::cout << result << std::endl;
	return 0;
}

1
2
3
4
main.cpp: In function ‘int main()’:
main.cpp:29:2: error: ‘else’ without a previous ‘if’
   29 |  else return 0;
      |  ^~~~

edit:fixed it with brackets.
Last edited on
The if statement only applies to the theplayer.number(). It doesn't apply to the cout statement. The else isn't matched hence the warning. What you really have is:

1
2
3
4
5
6
7
if(select=='a')
	theplayer.number();


std::cout << player.hp() << std::endl;
else return 0;


When the if (or else) is to have more than statement, then this needs to be done as a compound statement like this:

1
2
3
4
5
6
7
if(select=='a')
{
	theplayer.number();
	std::cout << player.hp() << std::endl;
}
else
    return 0;


using a pair of {} to specify the statements to be part of the if (or else) clauses.
Last edited on
Topic archived. No new replies allowed.