Structures and Arrays

Guys, I've been trying all day, and am lost. Please just point me in the right direction. Code first, explanation after.

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

struct Opponent{
	string name;
	int health;
	int attack;
};
Opponent samer[6] = { "Charizard", "Umbreon", "Mew", "Arcanine", "Espurr", "Luxaray" };

struct Mystats {
	string name;
	int health;
	int attack;
};
Mystats trainer[3] = { "Charizard", "Slowking", "Weavile" };


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


	samer[0].name = "Charizard"; //Samers Pokemon Stats
	samer[0].health = 20;
	samer[0].attack = 10;
	samer[1].name = "Umbreon";
	samer[1].health = 15;
	samer[1].attack = 10;
	samer[2].name = "Mew";
	samer[2].health = 25;
	samer[2].attack = 15;
	samer[3].name = "Arcanine";
	samer[3].health = 15;
	samer[3].attack = 15;
	samer[4].name = "Espurr";
	samer[4].health = 10;
	samer[4].attack = 15;
	samer[5].name = "Luxaray";
	samer[5].health = 10;
	samer[5].attack = 10;


	trainer[0].name = "Charizard"; //Trainers Pokemon stats
	trainer[0].health = 20;
	trainer[0].attack = 10;
	trainer[1].name = "Slowking";
	trainer[1].health = 30;
	trainer[1].attack = 6;
	trainer[2].name = "Weavile";
	trainer[2].health = 15;
	trainer[2].attack = 15;



	cout << "Musician Samer wants to battle!" "\n";
	cout << "Go, ";
	srand((unsigned)time(0));
	string samer_choice = Opponent samer[rand() % 6];
	cout << samer_choice << "!" << "\n";
	cout << "Please Choose your Pokemon: \n";
	cout << "1. Charizard", "2.Slowking", "3. Weavile \n";
	int my_choice;
	cin >> my_choice;
	cout << my_choice << ", I choose you! \n";
	cout << "\n";
	cout << "Would you like to attack? (yes or no) \n";
	getline(cin, mystr);

	system("PAUSE");

}


So, here is my code. At the moment, I'm literally, just trying to get a random selection for Samer, and an input for Trainer. Eventually, I want the two selections to "fight".

-In Opponent samer[6], it only accepts charizard and arcanine (maybe it likes fire types). It says it cannot convert from const char to int.

-Mystats trainer[3], only accepts charizard, same error.

-Opponent samer[rand() %6]- I can't figure out how to get this to run....I previously had a string called Samer, and a const char* with the pokemon, then set Samer=PokemonSamer[rand()%6], and that worked before.

Where in the hell did I go wrong? At the very least, just want to set back on the right path.
I'd move lines 14 and 21 into main to create the arrays of structs.

1
2
Opponent samer[6];
Mystats trainer[3];


You're already assigning all the values for name, health and attack in main. I think the error you're running into is because you're providing an initializer list of names, but not accounting for the other data members of the struct which are integers.

You have two declarations for samer_choice.
1
2
string samer_choice;
string samer_choice = Opponent samer[rand() % 6]; 

I think what you mean to have there is = samer[ran()%6].name ? to pull the name out of a random struct?

You have two different declarations for the same variable name.
1
2
string my_choice;
int my_choice;

I'm assuming you want to match the choice up to a name?
cout << my_choice << ", I choose you! \n";

So if you make my_choice an integer, you could do something like this

cout << trainer[my_choice-1].name << ", I choose you! \n";


Edit: Is this supposed to be one line? Take the commas from outside the quotes.

cout << "1. Charizard", "2.Slowking", "3. Weavile \n";

cout << "1. Charizard 2.Slowking 3. Weavile \n";
Last edited on
Lines 9 and 16: Your definitions for Opponnet and Mystat are identical. That's not necessarily a problem if you intend to make them different in the future.

Line 14: You're getting initialization errors because you're not allowing for the ints in the struct. You're also initializing the array of structs at line 33. You don't need to do it in both places. You can do it in just the initializer list and eliminate lines 33 - 50.
1
2
3
4
5
Opponent samer[6] = 
{ "Charizard", 10, 20,
   "Umbreon", 15, 10, 
// continue with the rest 
};


Line 68: You're trying to assign a struct to a string. That won't work. Remember what i told you in the other thread about saving the result of the rand() % 6 calculation.

1
2
3
  int samer_choice = rand() % 6;  // Save the computer's choide
  //  Now output the respective name
  cout << samer[samer_choice].name << "!" << "\n";

Last edited on
Thanks! I remembered it, was just getting lost somewhere. Opened a blank page and re-wrote it, no problems since. Did it look alright?

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;

struct Opponent{
	string name;
	int health;
	int attack;
};

struct Mystats {
	string name;
	int health;
	int attack;
};

Opponent samer[6] = {
	"Charizard", 20, 10,
	"Umbreon", 15, 10,
	"Mew", 25, 15,
	"Arcanine", 15, 15,
	"Espurr", 10, 15,
	"Luxaray", 10, 10
};

Mystats trainer[3] = {
	"Charizard", 20, 10,
	"Slowking", 30, 6,
	"Weavile", 15, 15
};

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

	cout << "Musician Samer wants to battle! \n";
	cout << "Go, ";
	srand((unsigned)time(0));
	samer_choice = rand() % 6;
	cout << samer[samer_choice].name << "!" << "\n";
	cout << "Please choose your Pokemon: \n";
	cout << "Press 1 for Charizard \n";
	cout << "Press 2 for Slowking \n";
	cout << "Press 3 for Weavile \n";
	getline(cin, mystr);
	stringstream(mystr) >> player_choice;
	cout << trainer[player_choice-1].name<< endl;

	system("PAUSE");
}
Looks fine.

Lines 53-54 you can simplify to:
1
2
 
  cin >> player_choice;

No need for the getline and stringstream.
oh.....cool. thanks!
Topic archived. No new replies allowed.