Accessing one element of array when Inputing

I am trying to access an element from an array I created in my code. I want the final output of my code to print the string of the Class the user types in.

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
 #include<iostream>
#include<string>
using namespace std;



int main()
{
	string CharacterName;
	int age;

	string Class[6] = { "Warrior", "Wizard", "Rogue", "Priest", "Paladin", "Mage" };

	cout << "What is your name?" << endl;
	getline(cin, CharacterName);

	cout << endl;
	cout << "Hello, " << CharacterName << ".  What is your age?" << endl;

	cin >> age;

	cout << "A fine age to be certain, " << CharacterName << ". And what is your class?" << endl;
	cout << "You may Choose from the following by typing the name of the class you will be chosing as it appears" << endl;
	cout << endl;
	cout << Class[0] << endl;
	cout << Class[1] << endl;
	cout << Class[2] << endl;
	cout << Class[3] << endl;
	cout << Class[4] << endl;
	cout << "or " << endl;
	cout <<Class[5] << endl;

	for (int i = 0; i < 6; i++)
		cin >> i;
	cout << endl;
	cout << "All hail " << CharacterName << ", age: " << age << ", the brave and migty " << Class << endl;

	

	system("pause");
}



everything up to line 34 seems to function properly, EXCEPT upon typing the sellection from the string array, the output ends with with the output for Class being some septidecimal number, like a memory address such as 00AFFB7C.


HOw can I correct this so that the final part of the ending printout prints to the screen the String the user types?

The loop on line 33 does not make sense, so remove it. It would make sense for enumerate the class names though.

the output ends with with the output for Class being some septidecimal number, like a memory address such as 00AFFB7C.
Indeed you output the address of the array while you want the selected class:

1
2
3
4
int i;
		cin >> i;
	cout << endl;
cout << "All hail " << CharacterName << ", age: " << age << ", the brave and migty " << Class[i] << endl; // Note [i] 


Also you should introduce error checking
so you mean create an enum for the class names?
No, I mean this:
1
2
3
4
5
6
7
8
for (int i = 0; i < (sizeof(Class) / sizeof(*Class)); i++) // Note: (sizeof(Class) / sizeof(*Class)) determines the correct size of the array (in case you want to change the array later)
{
  cout << i + 1 << ") " << Class[i] << endl;
}
int i;
		cin >> i;
	cout << endl;
cout << "All hail " << CharacterName << ", age: " << age << ", the brave and migty " << Class[i - 1] << endl; // Note: -1 due to the previous + 1 
Topic archived. No new replies allowed.