0

Trying to work on this code, and I keep getting the same error. by my research it's because the initialization supposedly is locked to case 1, but all the lines are initialised before any cases come into play. Maybe I'm just missing something?

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

int generateRandomStat() {
    return std::rand() % 16 + 3;
}

void saveCharacterToFile(const std::string &filename, const std::string &characterDetails) {
    std::ofstream outFile(filename);
    if (outFile.is_open()) {
        outFile << characterDetails;
        outFile.close();
        std::cout << "Character details saved to " << filename << std::endl;
    } else {
        std::cout << "Unable to save character details to " << filename << std::endl;
    }
}

void loadCharacterFromFile(const std::string &filename) {
    std::ifstream inFile(filename);
    if (inFile.is_open()) {
        std::string line;
        while (std::getline(inFile, line)) {
            std::cout << line << std::endl;
        }
        inFile.close();
    } else {
        std::cout << "Unable to load character details from " << filename << std::endl;
    }
}

int main() {
    std::srand(std::time(0));

    std::string characterName;
    std::string race, characterClass, background, backgroundDescription;
    int strength, dexterity, constitution, intelligence, wisdom, charisma;

    while (true) {
        std::cout << "Menu:" << std::endl;
        std::cout << "1. Generate New Character" << std::endl;
        std::cout << "2. Load Existing Character" << std::endl;
        std::cout << "3. Exit" << std::endl;

        int choice;
        std::cout << "Enter your choice: ";
        std::cin >> choice;

        switch (choice) {
            case 1:
                std::cin.ignore();
                std::cout << "Enter your character's name: ";
                std::getline(std::cin, characterName);

                std::cout << "Select a DnD 5e character race:" << std::endl;
                std::cout << "1. Dwarf" << std::endl;
                std::cout << "2. Elf" << std::endl;
                std::cout << "3. Halfling" << std::endl;
                std::cout << "4. Human" << std::endl;
                std::cout << "5. Dragonborn" << std::endl;
                std::cout << "6. Gnome" << std::endl;
                std::cout << "7. Half-Elf" << std::endl;
                std::cout << "8. Half-Orc" << std::endl;
                std::cout << "9. Tiefling" << std::endl;
            
                int raceChoice;
                std::cout << "Enter the number of your race choice: ";
                std::cin >> raceChoice;

                std::cout << "Select a DnD 5e character class:" << std::endl;
                std::cout << "1. Barbarian" << std::endl;
                std::cout << "2. Bard" << std::endl;
                std::cout << "3. Cleric" << std::endl;
                std::cout << "4. Druid" << std::endl;
                std::cout << "5. Fighter" << std::endl;
                std::cout << "6. Monk" << std::endl;
                std::cout << "7. Paladin" << std::endl;
                std::cout << "8. Ranger" << std::endl;
                std::cout << "9. Rogue" << std::endl;
                std::cout << "10. Sorcerer" << std::endl;
                std::cout << "11. Warlock" << std::endl;
                std::cout << "12. Wizard" << std::endl;

                int classChoice;
                std::cout << "Enter the number of your class choice: ";
                std::cin >> classChoice;

                std::cout << "Select a DnD 5e character background:" << std::endl;
                std::cout << "1. Acolyte" << std::endl;
                std::cout << "2. Charlatan" << std::endl;
                std::cout << "3. Criminal" << std::endl;
                std::cout << "4. Entertainer" << std::endl;
                std::cout << "5. Folk Hero" << std::endl;
                std::cout << "6. Guild Artisan" << std::endl;
                std::cout << "7. Hermit" << std::endl;
                std::cout << "8. Noble" << std::endl;
                std::cout << "9. Outlander" << std::endl;
                std::cout << "10. Sage" << std::endl;
                std::cout << "11. Sailor" << std::endl;
                std::cout << "12. Soldier" << std::endl;
                std::cout << "13. Urchin" << std::endl;

                int backgroundChoice;
                std::cout << "Enter the number of your background choice: ";
                std::cin >> backgroundChoice;

                strength = generateRandomStat();
                dexterity = generateRandomStat();
                constitution = generateRandomStat();
                intelligence = generateRandomStat();
                wisdom = generateRandomStat();
                charisma = generateRandomStat();

                std::string race;
                switch (raceChoice) {
                    case 1: race = "Dwarf"; break;
                    case 2: race = "Elf"; break;
                    case 3: race = "Halfling"; break;
                    case 4: race = "Human"; break;
                    case 5: race = "Dragonborn"; break;
                    case 6: race = "Gnome"; break;
                    case 7: race = "Half-Elf"; break;
                    case 8: race = "Half-Orc"; break;
                    case 9: race = "Tiefling"; break;
                    default: race = "Unknown Race"; break;
    }

                std::string characterClass;
                switch (classChoice) {
                    case 1: characterClass = "Barbarian"; break;
                    case 2: characterClass = "Bard"; break;
                    case 3: characterClass = "Cleric"; break;
                    case 4: characterClass = "Druid"; break;
                    case 5: characterClass = "Fighter"; break;
                    case 6: characterClass = "Monk"; break;
                    case 7: characterClass = "Paladin"; break;
                    case 8: characterClass = "Ranger"; break;
                    case 9: characterClass = "Rogue"; break;
                    case 10: characterClass = "Sorcerer"; break;
                    case 11: characterClass = "Warlock"; break;
                    case 12: characterClass = "Wizard"; break;
                    default: characterClass = "Unknown Class"; break;
    }

                std::string background;
                std::string backgroundDescription;
                switch (backgroundChoice) {
                    case 1: background = "Acolyte"; backgroundDescription = "You have spent your life in the service of a temple, learning sacred rites and ancient lore."; break;
                    case 2: background = "Charlatan"; backgroundDescription = "You have a talent for manipulating others, using your charm and wit to achieve your goals."; break;
                    case 3: background = "Criminal"; backgroundDescription = "You are skilled in deception, burglary, or other criminal activities, surviving through illicit means."; break;
                    case 4: background = "Entertainer"; backgroundDescription = "You thrive in the spotlight, entertaining others through music, acting, or other performance arts."; break;
                    case 5: background = "Folk Hero"; backgroundDescription = "You are a commoner who rose to fame for a heroic deed, inspiring others in your community."; break;
                    case 6: background = "Guild Artisan"; backgroundDescription = "You are a skilled artisan or craftsman, a member of a guild that shapes the world's culture."; break;
                    case 7: background = "Hermit"; backgroundDescription = "You have lived in seclusion, away from society, focusing on personal growth and enlightenment."; break;
                    case 8: background = "Noble"; backgroundDescription = "You were born into privilege, part of a noble family with connections and influence."; break;
                    case 9: background = "Outlander"; backgroundDescription = "You come from the wilds, surviving in nature's harshest conditions and mastering its secrets."; break;
                    case 10: background = "Sage"; backgroundDescription = "You are a scholar with a thirst for knowledge, seeking answers to life's greatest mysteries."; break;
                    case 11: background = "Sailor"; backgroundDescription = "You have sailed the seas, navigating the waters and facing the challenges of a life on a ship."; break;
                    case 12: background = "Soldier"; backgroundDescription = "You are a disciplined and trained soldier, serving in a military or mercenary company."; break;
                    case 13: background = "Urchin"; backgroundDescription = "You grew up on the streets, relying on your wits and agility to survive in a tough urban environment."; break;
                    default: background = "Unknown Background"; backgroundDescription = "Your character's background is a mystery."; break;
    }

                std::string characterDetails =
                    "Character Name: " + characterName + "\n" +
                    "Race: " + race + "\n" +
                    "Class: " + characterClass + "\n" +
                    "Background: " + background + "\n" +
                    "Background Description: " + backgroundDescription + "\n" +
                    "Strength: " + std::to_string(strength) + "\n" +
                    "Dexterity: " + std::to_string(dexterity) + "\n" +
                    "Constitution: " + std::to_string(constitution) + "\n" +
                    "Intelligence: " + std::to_string(intelligence) + "\n" +
                    "Wisdom: " + std::to_string(wisdom) + "\n" +
                    "Charisma: " + std::to_string(charisma);

                std::cout << characterDetails << std::endl;

                std::string filename = characterName + "_character.txt";
                saveCharacterToFile(filename, characterDetails);

                break;
            case 2:
                std::cin.ignore();
                std::cout << "Enter the filename of the character you want to load: ";
                std::string filename;
                std::getline(std::cin, filename);
                loadCharacterFromFile(filename);
                break;
            case 3:
                std::cout << "Exiting the program." << std::endl;
                return 0;
            default:
                std::cout << "Invalid choice. Please select a valid option." << std::endl;
        }
    }
}

Reviewed the code, and I don't see the problem. I've no real idea what exactly causes this, so no idea where to look.

Kla Veg
  • 13
  • 3
  • 1
    Case 1 has a huge amount of code in - try to pull it into a sperate function? Also, see https://stackoverflow.com/questions/5685471/error-jump-to-case-label-in-switch-statement – doctorlove Aug 22 '23 at 11:01
  • As I'm looking at it, the problem stems from skipping the initialization, but all the lines that the error throws at me, are initialized prior to running the switch. Does assigning a value to a string count as a separate initialization, in which case, it needs to be present for the other cases as well? I've read over the link provided, but that does not clear much up for me, in this case, my apologies. – Kla Veg Aug 22 '23 at 11:25
  • 1
    When case value of 2 is selected, can you point your finger at the exact line of the code that initializes `raceChoice`, `filename`, and all others, in that case? Just point your finger at that line, once you find it; but you will search hard and far, and you won't find it. Yes, the warning is bogus, but which part of it, exactly, you don't understand? Which lines, do you believe, initialize these variables, if the switch jumps past them? – Sam Varshavchik Aug 22 '23 at 11:33
  • Please read about the [mcve]. You could reproduce the problem in at most a dozen lines. – molbdnilo Aug 22 '23 at 12:32
  • Thank you @SamVarshavchik. I sat for a while, reading and rereading the code, and managed to get it to function. took some of the strings out of cases, and bracketed every case specifically, and now it functions. – Kla Veg Aug 22 '23 at 13:00

0 Answers0