A structure can be used to group several data types into place.
struct {
int num;
char letter;
bool isYes;
} structVar;Accessing a member of a structure requires the dot syntax.
struct {
int num;
char letter;
bool isYes;
} structVar;
structVar.num = 1;
structVar.letter = 'P';
structVar.isYes = False;In the example above, we simply assigned a value to a member of the struct structVar.
Using cout, we can output the values of the members of the struct using the dot notation as well:
cout << structVar.num << endl; // Outputs 1
cout << structVar.letter << endl; // Outputs 'P'
cout << structVar.isYes << endl; //Outputs FalseWe can also put a name to our structure, say this player information structure:
struct playerInfo {
int level;
float expAmt;
int strLevel;
int intLevel;
int magLevel;
int defLevel;
bool isDead;
bool isStandby;
bool isEating;
float hpAmt;
float mpAmt;
};We can use it as a data type.
playerInfo playerAlex;playerAlex now has the same structure as the playerInfo.
We can now assign a value to specific information abut playerAlex.
playerAlex.level = 10;
playerAlex.expAmt = 10.564;
playerAlex. strLevel = 7;
// ...
cout << playerAlex.level << endl; // Outputs 10
cout << playerAlex.strlevel << endl; // Outputs 7We can also do this for multiple variables, say if we have multiple players: playerSteve, playerChloe, playerSam.
playerInfo playerSteve;
playerInfo playerAlex;
playerInfo playerSam;We can now assign each of them their own specific player info based on the structure above.