Text - Based RPG - Object not being created?

I'm currently creating a text-based RPG, to get myself back into learning C++ again. The problem seems to be with the if-else ladder of statements, the user selects which race they wish to play as, which as a result creates the appropriate object for that race.

This is the character selection option code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::cout << "Please select a race: Human [1], Elf [2], Dwarf [3] or Orc [4]\n";
	std::cout << "Race selection: ";
	std::cin >> race_selection;
	std::cin.ignore();

	switch (race_selection) {

	case 1:
		std::cout << "You have selected Human\n";
		break;
	case 2:
		std::cout << "You have selected Elf\n";
		break;
	case 3:
		std::cout << "You have selected Dwarf\n";
		break;
	case 4:
		std::cout << "You have selected Orc\n";
		break;
	}


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
if (race_selection = 1) {

		//Create Human Object
		
		race Human(30, 50, 40, 20);

		std::cout << "Human object has been created";
		std::cin.get();
	}

	else if (race_selection = 2) {

		//Create a Elf object

		race Elf (15, 45, 35, 40);

		std::cout << "Elf object has been created";
		std::cin.get();
	}

	else if (race_selection = 3) {

		//Create a Dwarf object

		race Dwarf (60, 50, 50, 10);

		std::cout << "Dwarf object has been created";
		std::cin.get();
	}

	else if (race_selection = 4) {

		//Create a Orc object

		race Orc (70, 55, 45, 25);

		std::cout << "Orc object has been created";
		std::cin.get();
	}


The problem here is, regardless of which race I use using the above switch statement, the Human object always seems to be created, instead of the object for the desired race. Any ideas?
closed account (N36fSL3A)
Why don't you just combine the switch and if statements?

race Orc (70, 55, 45, 25);What is "race"?
When you declare an object within the context of the conditional statement, that object gets destroyed when the conditional code is over (i.e. the closing bracket). The objects you create will only be "visible" within the brackets of the conditional statements, but not outside. Since you're using the same "race" class for all the different races, why not just declare one in a higher namespace (such as the main function, or wherever most of your code is) and then just change the values within the conditional statements to make it match the player's selection?
ModShop is telling you the big problem here. You don't want these objects to be local to an if block.

That said... the problem you're asking about is because of this:

if (race_selection = 1) {

= is assignment.

You want == for comparison.
Thank you for everyone's responses I wasn't expecting such fast replies :)

I'll alter my code tomorrow morning, and thanks for helping learn from my mistakes.

@Lumpkin race is a class.

Topic archived. No new replies allowed.