Structures & Pointers (character profile)

Im looking to creat a small program to display 3 characters Zombie,Mage,Warrior,show there type,health and weapon,this is what i have so far as a test for the first one:
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
#include <iostream>

using namespace std;

struct characterone
{
 char type;
 int heal
 char weapon;
};

int main()
{
characterone zombie;

zombie.type = 'zombie';
zombie.health = 100;
zombie.weapon = 'melee';

cout<<"Character One is a: " << zombie.type << "\n";
cout<<"Character One health is " << zombie.health << "\n";
cout<<"Character Ones weapon is " << zombie.weapon << "\n";

cin.get();
cin.get();
return 0;
}


when i run this the name and weapon only show the letter 'e',not sure why?
I also need to be able to have the option of inputing a new health. If you can imagine this as being a Menu style,then you pick your character
Last edited on
I'd guess that 'e' stands for "error". Either that, or it only fetches the last character of your psuedostring.

Try using strings instead of chars. You'll need #include <string> .
http://cplusplus.com/reference/string/string/

For the menu, try using a switch statement.
http://cplusplus.com/doc/tutorial/control/

-Albatross
What Albatross said.
zombie.type = 'zombie'; When you try to assign more than one char to a char variable - you will only get the last char.

yeah,im not sure if its just this program or any compiler will do the same,how would i correct that or use it as a string instead?

im actually using QT creator if that makes any difference compared to lest say Visual basic
Use the string type instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>

string mytype;
string weapon;

int main()
{
    mytype = "zombie";
    weapon = "melee";

   cout << "Monster type is: " << mytype << "\n";
   cout << "Weapon type is: " << weapon << "\n";
   return 0;
}


Just an example on how to use the string.
thanks a lot,ill try throw that together with a small menu for just one character, also try figure how a user can re-enter a new health
I cant seem to get this to work,im trying to output the health but leave the option for it to be alterd up or down by a user input then re display again
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
#include <iostream>
#include <string>
using namespace std;

struct characterone
{
 string type;
 string weapon;
 char *health;
};

int main()
{
characterone zombie;

zombie.health = new char[50];
zombie.type = "Zombie";
zombie.weapon = "Melee";

cout<<"Character One is a: " << zombie.type << "\n";
cout<<"Character Ones weapon is: " << zombie.weapon << "\n";
cout<<"Character health is: " << zombie.health << "\n";
cin.get();
cin.get();
return 0;
}
Topic archived. No new replies allowed.