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.
if (race_selection = 1) {
//Create Human Object
race Human(30, 50, 40, 20);
std::cout << "Human object has been created";
std::cin.get();
}
elseif (race_selection = 2) {
//Create a Elf object
race Elf (15, 45, 35, 40);
std::cout << "Elf object has been created";
std::cin.get();
}
elseif (race_selection = 3) {
//Create a Dwarf object
race Dwarf (60, 50, 50, 10);
std::cout << "Dwarf object has been created";
std::cin.get();
}
elseif (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?
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?