# Thread: Need help with remembering numbers

1. ## Need help with remembering numbers

Ok, so Im making what I call a battle system. I think I pretty much know what I need to know to do it and I've already started working out the logistics of it, but Im not too sure on how you would get it to remember a number. They life points are 100, how do input this number and make its so it will remember whatever would be taken out of it?

2. const variables?

3. ## what?

Originally Posted by citizen
const variables?
what about them?

4. ## This is all I have so far

Code:
```#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int UHP[100]; // Users Hit points
int UATK = rand()%10; // Users Attack points
int UDFC = rand()%10; // Users Defence points
int UACC = rand()%10; // Users Accuracy points

int CHP[100]; // Challengers Hit points
int CATK = rand()%10; // Challengers Attack points
int CDFC = rand()%10; // Challengers Defence points
int CACC = rand()%10; // Challengers Accurasy points

cout<<"As your walking down a path a cloaked swordsman ambushes you.\n";
cout<<"He then challenges you to a fight and you humbly accept.\n";
cin.get();

}```
Heres what I was thinking when you type in attack it will see if you hit by ( UACC <= 6 ) if it is then you hit if not you miss. afterwards the challenger would get to use defence if their ( CACC <= 6 ) if not the dont get to defend and it would take the random number generated from UATK and subtract it from CHP. but i cant figure out how you would make it so it would keep it subtracted and not just reset back to 100

5. well life points would change eventually, no? You wouldn't want to make a const variable because it wouldn't change. What you could do is make a class (or struct) for a character, and the construction of the character class would set a member variable "health" to 100.

Edit: In your code example you are creating an array of 100 ints. That doesn't really represent a health value. Health is a whole number that would get reduced by whole numbers. There is no real way to use an array of 100 ints as a health value.

6. would you mind helping me with what I would do then? Im looking over the tuturials and im not seeing it and if i am its just not clicking.

7. Originally Posted by SebastionV3
would you mind helping me with what I would do then? Im looking over the tuturials and im not seeing it and if i am its just not clicking.
Health is a number, from 0 to how much health you have. Setting it up would be the same as the other variables you have there. What you want is a variable for your current health, and one for your maximum health. The same goes for the enemy.

int UHP = 100; int MUHP = 100;
int CHP = 100; int MCHP = 100;

Then you just take out what you want directly from UHP (current health). UHP = UHP - 10; would remove 10 health points. That could also be written as UHP -= 10; btw. So then the user then has 90/100 health (UHP = 90, and MUHP = 100).

8. It would look something like this
Code:
```struct character
{
//What the current health of the charcter is
int health;
//What they can have health wise
int max_health;
//Whatever else here
};

int main()
{
character player;

player.max_health = 100;
player.health = 100;

return 0;
}```

9. ok yeah haha i have no idea how to work structures yet. kind of only going off the tuturials in this website doesnt exactly teach you everything you need to know.

10. Originally Posted by SebastionV3
ok yeah haha i have no idea how to work structures yet. kind of only going off the tuturials in this website doesnt exactly teach you everything you need to know.
Nope, but it does teach classes (which are the same as structures in C++). Made it to Lesson 12? You'll get it!

11. yeah ill figure it out sooner or later. I hit c++ pretty hard and only took me maybe half an hour to figure out enough to make some chat bots and deathclocks and a few death games that I made up

12. Originally Posted by prog-bman
It would look something like this
Code:
```struct character
{
//What the current health of the charcter is
int health;
//What they can have health wise
int max_health;
//Whatever else here
};

int main()
{
character player;

player.max_health = 100;
player.health = 100;

return 0;
}```
Now is the structure part global?

13. player is just an instance of a type character.

14. Originally Posted by SebastionV3
Now is the structure part global?
The struct can be accessed anywhere in that file yes. If you declared it in a header file, and included that header file you could use it too. So it is global, but it's generally not referred as global, since thats usually how they are. It's just a data type, like floats and ints, and you have to make an instance of it. Which is what character player; does. player is an instance of character and its scope (can only be used in) is in main() only. You could make it global by removing that part from main(), and doing this:
Code:
```struct character
{
...
};

character player;```
or the shortcut:
Code:
```struct character
{
...
} player;```
However, there are better alternatives (eg. passing references/pointers). This approach is fine for now, but just keep that in mind.

15. Just think of a struct as a new type, like int or float. This behaviour is emphasized even more in C++ classes. You can create instances of type int and float. You can do the same with structs and classes as well. Variables of user-defined types generally conform to the same basic rules as their built-in counterparts, including scope rules, memory allocation and the works.

Popular pages Recent additions