If statements help

I'm practicing writing some codes for a video game. I'm starting off having the player have an option, but the "Choose a Class" part doesn't work. Any ideas?

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
string name;
int choice;

cout << "Enter Your Name: ";
cin >> name;

cout << "Choose a Class:" << endl;
cout << "1. Fighter" << endl;
cout << "2. Mage" << endl;
cout << "3. Healer" << endl;
cout << "4. Hunter" << endl;
cin >> choice;

if (choice == 1)
{
cout << "You've Choosen Fighter";
}

if (choice == 2)
{
cout << "You've Choosen Mage";
}

if (choice == 3)
{
cout << "You've Choosen Healer";
}

if (choice == 4)
{
cout << "You've Choosen Hunter";
}

return 0;
}
Last edited on
I see that name is a double. Are you entering a number as the name, or are you trying to enter letters? If you want the name to be letters, then you should make name of type string; not double.
The only issue I see with the code you have supplied is that you are using the string data type without #include <string>. Outside of that I would recommend that you use a switch statement instead of a series of if statements. Please see the rework 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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string name;
	int choice;

	cout << "Enter Your Name: ";
	cin >> name;

	cout << "Choose a Class:" << endl;
	cout << "1. Fighter" << endl;
	cout << "2. Mage" << endl;
	cout << "3. Healer" << endl;
	cout << "4. Hunter" << endl;
	cin >> choice;

	switch (choice)
	{
	case 1:
		cout << "You've Choosen Fighter \n";
		break;

	case 2:
		cout << "You've Choosen Mage \n";
		break;

	case 3:
		cout << "You've Choosen Healer \n";
		break;

	case 4:
		cout << "You've Choosen Hunter \n";
		break;
	}
	system("Pause");

	return 0;
}




Topic archived. No new replies allowed.