c++ - 从 while 循环中断后似乎没有进一步的代码执行 (c++)

标签 c++ while-loop break

我正在学习 C++ 并创建一个小型角色扮演游戏。

我有一个提供初始统计点的函数,但是一旦添加了所有点,我似乎无法跳出 while 循环并在循环后执行 cout。请注意,所有技能名称和角色名称都是前向声明的,并且技能名称是完整键入的。 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";
}

最佳答案

我假设您指的是这个特殊的休息时间。 if 语句将 0 赋值给 RemainingPoints,因此永远不会计算为真。您应该使用 ==,这是一个常见的错字。

    if(RemainingPoints = 0)
    {
        break;
    }

关于c++ - 从 while 循环中断后似乎没有进一步的代码执行 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436975/

相关文章:

Objective-C NSString 的 Run while 语句

c++ - 为什么要通过 const 引用而不是按值传递?

c# 循环直到 Console.ReadLine = 'y' 或 'n'

c++ - 如何从掷骰子中找到最小值和最大值?

c - Putchar、getchar、while 循环中缺少字符

java - 如果用户输入的整数不符合我的条件,如何结束控制台?

c++ - 返回false后是否需要break

c# - 使用 'goto' 语句不好吗?

c++ - Win32 Combobox,打印选中编辑文本框的名称

c++ - 具有 volatile 原子变量的原子操作