看来在从while循环中断后没有其他代码执行(c ++)

时间:2013-12-07 02:49:59

标签: c++ while-loop break

我正在学习c ++并创建一个小型的RPG游戏。

我有一个函数来给出初始的stat点,但是一旦添加了所有的点,我似乎无法突破while循环并在循环后执行cout。请注意,所有技能名称和CharacterName都是向前声明的,技能名称是完整的。 RemainingPoints也是前向声明的。全部继续正确执行。也没有编译器错误。

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";
}

2 个答案:

答案 0 :(得分:2)

我认为你指的是这个特殊的突破。 if语句是0到RemainingPoints的赋值,因此永远不会计算为true。你应该使用==,这是一个常见的拼写错误。

    if(RemainingPoints = 0)
    {
        break;
    }

答案 1 :(得分:2)

if(RemainingPoints = 0)
{
   break;
}

这就是为什么写

更好的原因
if(0 = RemainingPoints)

并且有编译器错误而不是恼人的错误