Conspire
Conspire

Reputation: 1

Appears that no further code executes after break from while loop (c++)

I'm learning c++ and creating a small rpg-esque game.

I've got a function to give initial stat points, however once all of the points are added I seem to be unable to break out of the while loop and execute the cout after the loop. Please note that all the skill names and CharacterName are forward declared and the names of skills are typed in full. RemainingPoints is also forward declared. All continues execute correctly. There are also no compiler errors.

void CharacterSkillSetup()
{
    std::string SelectedSkill;
    int AssignedPoints;

    while(SelectedSkill != "Muscle" || SelectedSkill != "Focus"
        || SelectedSkill != "Reflexes" || SelectedSkill != "Cogni"
        || SelectedSkill != "Precision" || SelectedSkill != "Resistance"
        || SelectedSkill != "Luck" || SelectedSkill != "Finished")

    {
        std::cout << "\nWell then " << CharacterName
            << ", you seem like you have the capacity for a bit of training. \n \n";
        std::cout << "You can train in the following skills: \n";
        std::cout << "Muscle " << MuscleSkill << " - Increase Physical and Ranged damage. \n";
        std::cout << "Focus " << FocusSkill << " - Increase Magical damage. \n";
        std::cout << "Reflexes " << ReflexesSkill << " - Increase Physical and Ranged speed. \n";
        std::cout << "Cogni " << CogniSkill << " - Increase Magical speed. \n";
        std::cout << "Precision " << PrecisionSkill << " - Increase hit chance. \n";
        std::cout << "Resistance " << ResistanceSkill
            << " - Increase hitpoints and lessen damage received. \n";
        std::cout << "Luck " << LuckSkill << " - Increase value of loot drops. \n \n";
        std::cout << "You currently have " << RemainingPoints << " skillpoints to assign. \n \n";
        std::cout <<
            "Please type the full name of the skill followed by how many points you wish \nto assign to it.\n";
        std::cout << "\nIf you wish to assign some skillpoints at a later time type 'Finished 0'\n";
        std::cin >> SelectedSkill >> AssignedPoints;
        if(std::cin.fail())
        {
            std::cin.clear();
        }

        if(AssignedPoints > 14 || AssignedPoints < 0 || (RemainingPoints - AssignedPoints < 0))
        {
            AssignedPoints = 0;
        }

        if(SelectedSkill == "Muscle")
        {
            MuscleSkill = MuscleSkill + AssignedPoints;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Focus")
        {
            FocusSkill = AssignedPoints + FocusSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Reflexes")
        {
            ReflexesSkill = AssignedPoints + ReflexesSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Cogni")
        {
            CogniSkill = AssignedPoints + CogniSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Precision")
        {
            PrecisionSkill = AssignedPoints + PrecisionSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Resistance")
        {
            ResistanceSkill = AssignedPoints + ResistanceSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Luck")
        {
            LuckSkill = AssignedPoints + LuckSkill;
            RemainingPoints = RemainingPoints - AssignedPoints;
        }

        else if(SelectedSkill == "Finished")
        {
            std::string Choice;

            std::cout << "Are you sure you're finished adding points? \n";
            std::cin >> Choice;

            if(Choice == "Yes")
            {
                break;
            }
            else if(Choice == "No")
            {
                continue;
            }
        }

        if(RemainingPoints = 0)
        {
            break;
        }
    }

    std::cout << "Loop finished";
}

Upvotes: 0

Views: 105

Answers (2)

Yury Korobkin
Yury Korobkin

Reputation: 115

if(RemainingPoints = 0)
{
   break;
}

That's why it's better to write

if(0 = RemainingPoints)

and have a compiler error instead of an annoying bug

Upvotes: 2

tim
tim

Reputation: 632

I assume you are referring to this particular break. The if statement is an assignment of 0 to RemainingPoints and so will never evaluate to true. You should use ==, a common typo.

    if(RemainingPoints = 0)
    {
        break;
    }

Upvotes: 2

Related Questions