Declaring variables problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void Barbariana()
{
short level=1;
int strenght=25;
int dextery=10;
int vitality=20;
int mana=15;
int damage=strenght*3;
int att=dextery*10;
int block=dextery-level;
int defence=dextery*9;
int life=(vitality*3)+60;
int manapoints=mana*2;
}

int main()
{
Barbarian();
cout<<"your level is"<<level;
cin.get();
}


This is only to check if it would work.It didn't.
Is there a way in which i can declare these variables inside my function?
This was ment to be the first of my characters and i would prefer if the same-name variables would be used for every other function and applied when the user decides what character he wants to play.
Any ideas?
You probably want to create a characters class that contain all the variables of the character.
If you don't know how to make classes yet, try a struct:
http://www.cplusplus.com/doc/tutorial/structures/
So,should i change the void to struct or add this struct statement inside of my statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct character
{
    short level;
    int strenght;
    int dextery;
    int vitality;
    int mana;
    int damage;
    int att;
    int block;
    int defence;
    int life;
    int manapoints;
}

int main()
{
    character Barbarian; //create a struct called "Barbarian"

    //access variables with dot notation ( . )
    cout<<"your level is"<< Barbarian.level;
    cin.get();
}


You'll need to set all the values before use:
Barbarian.level = 1;
Missing semicolon on line 14.

Also you can assign its default values with the list initializer syntax:
character Barbarian = {/*values for the fields, in order, comma separated*/};
Last edited on
My bad, I typed that out in firefox.
closed account (zb0S216C)
Since no-one else mentioned it: If you're using structures, or classes for that matter, use constructors[1] for member initialization.

1
2
3
4
5
6
7
8
9
10
struct character
{
    character();

    // ...
};

character::character()
    : level(0), strength(0), /*...*/
{ }


References:

[1] http://www.cplusplus.com/doc/tutorial/classes/


Wazzak
I've only ever used structs about 2 times, and I never knew you could do that with them.

I'm guessing you'd have a normal struct, as above, and where you have the "// ..." you'd have your struct variables. And then you'd call the constructor right after the creation of the struct?
"struct" and "class" are exactly the same in C++ except that struct defaults to public and class defaults to private. Unions can also do most of the same things as classes and structs.
Ah right. thanks. That's why I like this forum, you try to teach something and end up getting taught! :D
Last edited on
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
#include <iostream>
using namespace std;

///***************************************************************************************************
short level;
int strenght;
int dextery;
int vitality;
int mana;
int damage1;
int damage2;
int att;
int block;
int defence;
int life;
int manapoints;
int weaponblock;
int magicblock;
///***************************************************************************************************
void Barbarian()
{
level=1;
strenght=25;
dextery=10;
vitality=20;
mana=15;
damage1=3*strenght;
damage2=3*(dextery);
att=10*dextery;
block=dextery-level;
defence=9*dextery;
life=60+(3*vitality);
manapoints=2*mana;
}

int main()
{
Barbarian();
cout<<level<<endl<<strenght<<endl<<dextery<<endl<<vitality<<endl<<mana<<endl<<damage1<<endl<<damage2<<endl<<att<<endl<<weaponblock<<endl;
cout<<defence<<endl<<life<<endl<<manapoints;
cin.get();}


Thats how you do it guys :D
sasukebartioso wrote:
Thats how you do it guys :D

Good! Now I know.

I hope you never going to have more than one character at the same time.
closed account (zb0S216C)
sasukebartioso wrote:
Thats how you do it guys (sic)

The best implementation I've seen so far. I especially love the heavy utilization of the global namespace.

Wazzak
Last edited on
sasukebartioso wrote:
Thats how you do it guys

orly? Congrats
why didnt ye tell me that before??Cuz it took me a while to figure thhat out.
and i had to say that :P

i will only have one character per game sesion.
shoul i make mobs in the same way just with different variables??
and then just set the variables i used to nothing after the mob is defeated??

Whats going on with these structure and class things btw?I tried to use them but it said that you cant assign variables to numbers.Why is that??
Last edited on
Referring to my previous post...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct character
{
    short level;
    int strenght;
    int dextery;
    int vitality;
    int mana;
    int damage;
    int att;
    int block;
    int defence;
    int life;
    int manapoints;
}

int main()
{
    character Barbarian; //create a struct called "Barbarian"

    //access variables with dot notation ( . )
    cout<<"your level is"<< Barbarian.level;
    cin.get();
}


You'll need to set all the values before use:
Barbarian.level = 1;


Barbarian.level = 1; would be within main, or where ever you create the object.

Here's code, using a struct and using a function to get the values for it:
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
59
60
61
#include <iostream>		// cout
#include <conio.h>		// _kbhit()


struct character
{
    short level;
    int strenght;
    int dextery;
    int vitality;
    int mana;
    int damage;
    int att;
    int block;
    int defence;
    int life;
    int manapoints;
};


//pass the character struct by reference
//so that the value set are the same in the function that called it,
//in this case, main()
void createCharacter( character &c, const int i )
{
	switch( i )
	{
		//player
		case 1:
			c.level = 20;
			/* all other stats */
			break;

		//enemy
		case 2:
			/* stats for enemies */
			break;
	}
}


int main()
{
	character Barbarian; //create a struct called "Barbarian"

	//get values
	//if you were creating an enemy, you would pass
	// 2 instead of 1 to the function
	createCharacter( Barbarian, 1 );

	//access variables with dot notation ( . )
	std::cout << "your level is " << Barbarian.level;

	//press any key to exit...
	std::cout << "\n\nPress any key to exit...";

	while( ! _kbhit() )
	{ /* do nothing - wait for key press */ }
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.