Questions about how a Structure works

Code First, will be easier to explain later.

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
//Battle with Samer
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

const char* PokemonSamer[6] = {
"Charizard", "Umbreon", "Mew", "Arcanine", "Espurr", "Luxaray"
};

struct stats {
int health
int attack
};
stats Charizard, Umbreon, Mew, Arcanine, Espurr, Luxaray, Slowking, Weavile;

int main()
{
string mystr;
string Samer;
string Trainer;

Charizard.health=25;
Charizard.attack-15;
Umbreaon.health=20;
Umbreaon.attack=10;
Mew.health=30;
Mew.attack=20;
Arcanine.health=20;
Arcanine.attack=20;
Espurr.health=15;
Espurr.attack=25;
Luxaray.health=18;
Luaray.attack=20;
Slowking.health=45;
Slowking.attack=8;
Weavile.health=15;
Weavile.attack=25;

cout << "Musician Samer wants to battle!" "\n";
cout << "Go, ";
srand((unsigned)time(0));
Samer=PokemonSamer[rand() % 6];
cout << Samer << "!" << "\n";
cout << "Please Choose your Pokemon: \n";
cout << "Charizard, Slowking, Weavile \n";
getline(cin, mystr);
stringstream(mystr) >> Trainer;
cout << Trainer << ", I choose you! \n";
cout << "\n";
cout << "Would you like to attack? (yes or no) \n";
getline(cin, mystr);


system("PAUSE");
}


So, here is where I'm a little lost. I have 6 items listed as <Const Char* PokemonSamer>. One of them, will be assigned to the <String Samer>, and one to <String Trainer>. After those assignments are done, will I be able to reference the items in the structure using Samer and Trainer (Samer.health, Trainer.attack)?

Or do I need to use an Array to do that? (Still do not fully understand Array's and structures. Re-reading)
Last edited on
You have a couple of problems you need to solve at this point.

1) At line 45 you select a random name from the list of names. You want to save that random value. You'll need that random value to find the attack points for that character.

2) At line 48, you ask the player to select one of three names, but you don't check that they selected one of the three. You also need to find the index into the array of names to find that character's hit points and health.

I would suggest changing your list of names and stats into an array of players.
1
2
3
4
5
6
7
8
9
10
11
12
struct Player
{  string name;
    int health
    int attack
};
Player player[6];  // Allocate 6 players
...
//  Initialize the players 
player[0].name = "Charizard";
player[0].health=25;
player[0].attack-15;
//  Continue with initializing players [1]-[5]. 


Since you want to limit the user to 3 choices, you might want to reorder your characters so that the 3 available to the user are [0]-[2]. Now, you can change lines 45-49 as follows:
1
2
3
4
5
  int computer_choice = rand() % 6;
  cout << "1. Charizard, 2. Slowking, 3. Weavile \n";
  int user_choice;
  cin >> user_choice;
  //  Check that user enters 1-3, then decrement to refer to [0]-[2]. 


Now, your battle becomes simply:
1
2
3
4
5
6
  while (player[computer_choice].health > 0 && player[user_choice].health > 0) 
  {  // Both players still alive
      player[computer_choice].health -= player[user_choice].attack;
      player[user_choice].health -= player[computer_choice].attack;
  }
  //  One of the players is dead.  Declare the other one the winner 




Damn. That is much cleaner, and makes sense. Maybe you could explain somethings to me.

1) Are you using int computer_choice=rand() %6 because we can assign a number, and it's easier to call up, as opposed to letting Samer=rand?

2)With the use of rand() %6, do i still need to have srand((unsigned)time(0)) in there as well?

Thanks again for the advice!
1) yes. See my point #1 above.
2) yes. You always want to initialize the random number generator once and only once.
Excellent. Thank you for all of your help!!!
Topic archived. No new replies allowed.