So I am beginning to learn and learnt a bit.
Now just as practice, trying to create a simple game where one monster attacks another, well, that's too big aim, running into troubles already.
Will use this thread for queries related to it.
So now just starting with check stats part
It works right if I do it this way:
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
#include <string>
using namespace std;
enum MonsterType {
OGRE,
DRAGON,
ORC,
GIANT_SPIDER,
SLIME
};
struct Monster {
MonsterType type;
string name;
int health;
};
string GetMonsterTypeString(Monster monster){
if(monster.type == OGRE)
return "Ogre";
if(monster.type == DRAGON)
return "Dragon";
if(monster.type == ORC)
return "Orc";
if(monster.type == GIANT_SPIDER)
return "Giant Spider";
if(monster.type == SLIME)
return "Slime";
return "Unknown";
}
void PrintMonster(Monster monster){
cout << "This " << GetMonsterTypeString(monster);
cout <<" is named " << monster.name << " and has " << monster.health << " health.\n";
}
int main()
{
Monster ogre = {OGRE, "Torg", 145};
Monster slime = {SLIME, "Blurp", 23};
Monster dragon = {DRAGON, "Mini", 50};
Monster giantSpider = {GIANT_SPIDER, "Momba", 80};
Monster orc = {ORC, "Bob", 15};
cout<<"Choose activity: \n1. Check stats \n2. Attack";
int activity;
cin>>activity;
switch(activity)
{
case 1:
{
cout<<"Enter monster you want to get details of: \n1. ogre \n2. dragon \n3. orc \n4. gaint spider \n5. slime";
int getDetails;
cin>>getDetails;
switch(getDetails)
{
case 1:
PrintMonster(ogre);
break;
case 2:
PrintMonster(dragon);
break;
case 3:
PrintMonster(orc);
break;
case 4:
PrintMonster(giantSpider);
break;
case 5:
PrintMonster(slime);
break;
}
}
case 2:
cout<<"Coming soon";
}
return 0;
}
|
But that's too confusing in main I guess.
How do I seperate the checkStats thing into a different function.
I tried doing this
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <string>
using namespace std;
enum MonsterType {
OGRE,
DRAGON,
ORC,
GIANT_SPIDER,
SLIME
};
struct Monster {
MonsterType type;
string name;
int health;
};
string GetMonsterTypeString(Monster monster){
if(monster.type == OGRE)
return "Ogre";
if(monster.type == DRAGON)
return "Dragon";
if(monster.type == ORC)
return "Orc";
if(monster.type == GIANT_SPIDER)
return "Giant Spider";
if(monster.type == SLIME)
return "Slime";
return "Unknown";
}
void PrintMonster(Monster monster){
cout << "This " << GetMonsterTypeString(monster);
cout <<" is named " << monster.name << " and has " << monster.health << " health.\n";
}
void checkStats()
{
cout<<"Enter monster you want to get details of: \n1. ogre \n2. dragon \n3. orc \n4. gaint spider \n5. slime";
int getDetails;
cin>>getDetails;
switch(getDetails)
{
case 1:
PrintMonster(ogre);
break;
case 2:
PrintMonster(dragon);
break;
case 3:
PrintMonster(orc);
break;
case 4:
PrintMonster(giantSpider);
break;
case 5:
PrintMonster(slime);
break;
}
int main()
{
Monster ogre = {OGRE, "Torg", 145};
Monster slime = {SLIME, "Blurp", 23};
Monster dragon = {DRAGON, "Mini", 50};
Monster giantSpider = {GIANT_SPIDER, "Momba", 80};
Monster orc = {ORC, "Bob", 15};
cout<<"Choose activity: \n1. Check stats \n2. Attack";
int activity;
cin>>activity;
switch(activity)
{
case 1:
checkStats();
case 2:
cout<<"Coming soon";
}
return 0;
}
|
If I simply move it, compiler starts complaining that ogre, orc, dragon, etc are not defined in this(in the seperate checkStats function) scope.
So it seems I need to define the Monsters in that function again, no, this should be wrong then. I need the function to use the values defined in main. How?