Assign Array to Variable?

How do I make it so std::string TheArray = LegendArray[SelectedLegend1]; works? I'm trying to make it cout the output of the randomized array as shown below.

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
  void RemoveSecond() {
	std::string AlreadyPicked = "";
	std::cout << "Champion already picked?: ";
	std::getline(std::cin, AlreadyPicked);
	if (AlreadyPicked[0] == 'b' || AlreadyPicked[0] == 'B' && AlreadyPicked[1] == 'l' || AlreadyPicked[1] == 'L') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'g' || AlreadyPicked[0] == 'G' && AlreadyPicked[1] == 'i' || AlreadyPicked[1] == 'I') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Lifeline","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'l' || AlreadyPicked[0] == 'L' && AlreadyPicked[1] == 'i' || AlreadyPicked[1] == 'I') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Pathfinder","Wraith","Bangalore","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'p' || AlreadyPicked[0] == 'P' && AlreadyPicked[1] == 'a' || AlreadyPicked[1] == 'A') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Wraith","Bangalore","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'w' || AlreadyPicked[0] == 'W' && AlreadyPicked[1] == 'r' || AlreadyPicked[1] == 'R') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Bangalore","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'b' || AlreadyPicked[0] == 'B' && AlreadyPicked[1] == 'a' || AlreadyPicked[1] == 'A') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Caustic","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'c' || AlreadyPicked[0] == 'C' && AlreadyPicked[1] == 'a' || AlreadyPicked[1] == 'A') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Mirage" };
		return;
	}
	else if (AlreadyPicked[0] == 'm' || AlreadyPicked[0] == 'M' && AlreadyPicked[1] == 'i' || AlreadyPicked[1] == 'I') {
		srand(time(0));
		int SelectedLegend1 = rand() % 7;
		const std::string LegendArray[] = { "Bloodhound","Gibraltar","Lifeline","Pathfinder","Wraith","Bangalore","Caustic" };
		return;
	}
	else {
		std::cout << "Incorrect answer try again.";
		RemoveSecond();
	}
	std::string TheArray = LegendArray[SelectedLegend1];
Why isn't it working? What does your compiler tell you?
'LegendArray' : undeclared identifier
'SelectedLegend1' : undeclared identifier
Several things, first srand() should only be called once, usually in main().

Second SelectedLegend1 is local to each of those if() statements. If you want it available outside the if() statement you need to create the instance before the if() statements and then assign a value to that value inside those if() statements.

What is with all of those returns?

Do you realize that "TheArray" is only created if the final else statement executes.

EDIT: You also should do some research on variable scoping in C++.
Last edited on
Topic archived. No new replies allowed.