ReznoV ReznoV - 2 months ago 8
C++ Question

Passing struct to function without typing every member (C++ no pointers)

So I think more of the inexperienced programmers will be roaming around with this question. I can pass a struct to a function successfully like the snippet below:

void printMonster(MonsterStats slime, MonsterStats spider, MonsterStats orc, MonsterStats ogre, MonsterStats dragon, int charLevel)


However you could imagine if the struct MonsterStats had 120 members which all needed to be passed to printMonster my code will become absolute spaghetti!

So the question is, how can I pass this struct into my function without typing every single member into the function parameter?

For inquiries about how much I "should" know about c++ at this point, I have arrived at the learncpp.com chapter 4 comprehensive quiz. I went a little out of scope of the assignment but I've done nothing I shouldn't be able to do by now.

full below (working but lengthy):

#include <iostream>


struct MonsterStats
{
std::string type;
std::string name;
int health;
int level;
};



void printMonster(MonsterStats slime, MonsterStats spider, MonsterStats orc, MonsterStats ogre, MonsterStats dragon, int charLevel)
{

if(charLevel>=slime.level&&charLevel<spider.level)
std::cout << "This " << slime.type << " is named " << slime.name << " and has " << slime.health << " health.\n";
else if(charLevel>=spider.level&&charLevel<orc.level)
std::cout << "This " << spider.type << " is named " << spider.name << " and has " << spider.health << " health.\n";
else if(charLevel>=orc.level&&charLevel<ogre.level)
std::cout << "This " << orc.type << " is named " << orc.name << " and has " << orc.health << " health.\n";
else if(charLevel>=ogre.level&&charLevel<dragon.level)
std::cout << "This " << ogre.type << " is named " << ogre.name << " and has " << ogre.health << " health.\n";
else if(charLevel>=dragon.level)
std::cout << "This " << dragon.type << " is named " << dragon.name << " and has " << dragon.health << " health.\n";
}

int main()
{
using namespace std;

MonsterStats ogre{"Ogre","Grumble",345, 16}; //Type, Name, Health, Level
MonsterStats dragon{"Dragon","Spyro", 890, 21}; //Type, Name, Health, Level
MonsterStats orc{"Orc","Rakanishu", 165, 11}; //Type, Name, Health, Level
MonsterStats spider{"Giant Spider", "Arachnid", 80, 6}; //Type, Name, Health, Level
MonsterStats slime{"Slime", "Blurp", 35, 1}; //Type, Name, Health, Level

std::string getChoice="y";
int charLevel;
cout << "Please enter the level of your character to proceed to battle: ";
cin >> charLevel;
if(charLevel>0)
{
while(getChoice=="y")
{
printMonster(slime, spider, orc, ogre, dragon, charLevel);

cout << "Do you want to fight the monster? (y/n)";
cin >> getChoice;
if(getChoice=="y")
{
cout << "You destroyed the monster and gained an experience level!\n\n";
charLevel+=1;
}
else
cout << "You ran like Forest Gump and got out of the fight safely!\n\n";
}
}
else
cout << "Please create a character in order to play the game.";
return 0;
}


Thanks in advance for the answers :)

Answer

What you tend to call "members" (e.g. ogre, spider, etc...) are in fact instances of the MonsterStats structure. So variables.

In C++, the "members" are the variables or functions that are in the structure (e.g.name, health, level, etc...).

Of course, using many instances like in your snippet, and retype a lot of time the same things would be a nightmare. Fortunately, in chapter 5 you'll see the loops and in chapter 6 you'll learn about arrays and vectors to handle a bundle of instances.

This would look like:

#include <iostream>
#include <string>
#include <vector>

struct MonsterStats
{
   ...
};
...
int main()
{
    using namespace std;

    vector<MonsterStats> monsters { 
                            {"Ogre","Grumble",345, 16}, 
                            {"Dragon","Spyro", 890, 21}, 
                            {"Orc","Rakanishu", 165, 11}, 
                            {"Giant Spider", "Arachnid", 80, 6}, 
                            {"Slime", "Blurp", 35, 1} }; 

    ... 
    printMonster(monsters, charlevel); 
    ...
}

And you would then access to the i-th item with monsters[i].