You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++方向输入验证失效:While循环未触发错误提示的排查

问题描述

我尝试使用while循环实现输入验证,确保用户无法输入north、south、east、west之外的方向。代码可正常编译,但无论输入什么方向都无法触发“Invalid entry”提示,请问问题出在哪里?

相关代码

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main() {

    cout << "********************Welcome to the COSC-1436*******************\n";
    cout << "******************Version of the Classic Text Game*****************\n";
    cout << "*****************************Zork*****************************\n";

    double kilometers = 25;
    double miles = kilometers * 0.62137;
    cout << "***************This map covers " << kilometers << " km" << "/" << setprecision(7) << miles << " miles***************" << endl;


    string direction; // direction user would like to move.
    double steps; // user's number of steps.
    const double STEPS_PER_MILE = 2000; // steps per mile.
    const int STEPS_TO_FOREST = 50; // number of steps to the forest.
    const int STEPS_TO_DUNGEON = 100; // number of steps to the dungeon.
    const int STEPS_TO_HOUSE = 4000; // number of steps to the house.
    const int STEPS_TO_MAZE = 6000; // number of steps to the maze.
    string north;
    string south;
    string east;
    string west;
    int menuChoice;


    //Ask user what action they would like to take and display menu.
    cout << "\nWhich action would you like to take?\n\nPlease choose a number from the menu options below and then click 'enter':\n\n";
    cout << "1. Move\n";
    cout << "2. Attack\n";
    cout << "3. Pick up Item\n";
    cout << "4. Quit\n";
    cin >> menuChoice;

    switch (menuChoice)
    {
    case 1: cout << "You chose option 1. Move\n";
    {
        cout << "What direction would you like to move?\n"; //Get the direction user would like move
        cin >> direction;
        while (direction != north && direction != south && direction != east && direction != west)
        {
            cout << "Invalid entry! "
                 << "Please enter a direction that is either north, south, east, or west.\n";
            cin >> direction;

        }
        cout << "How many steps are you taking?\n"; //Get the number of steps user is taking.
        cin >> steps;
        while (steps < 0)
        {
            cout << "Invalid entry!"
                << " Please enter a number greater than 0.\n";
            cin >> steps;
        }
            double milesMoved = steps / STEPS_PER_MILE;
            double kilometersMoved = milesMoved / 0.62137;

            //Determine and display result.
            cout << "\nYou walked " << milesMoved << " miles and " << kilometersMoved << " kilometers " << direction << ".\n";


            //If the number of steps is greater than or equal to the number of steps to reach the forest, and the user is moving north.
            if (steps >= STEPS_TO_FOREST && direction == "north")
                cout << "\nYou have now entered the forest.\n";

            //If the number of steps is greater than or equal to the number of steps to reach the dungeon, and the user is moving south.
            else if (steps >= STEPS_TO_DUNGEON && direction == "south")
                cout << "\nYou have now entered the dungeon.\n";

            //If the number of steps is greater than or equal to the number of steps to reach the house, and the user is moving east.
            else if (steps >= STEPS_TO_HOUSE && direction == "east")
                cout << "\nYou have now entered the house.\n";

            //If the number of steps is greater than or equal to the number of steps to reach the maze, and the user is moving west. 
            else if (steps >= STEPS_TO_MAZE && direction == "west")
                cout << "\nYou have now entered the maze.\n";

            else
                cout << "\nYou are now lost.\n";
        }
            break; 

    case 2: cout << "You chose option 2. Attack\n";
            break;  
    case 3: cout << "You chose to pick up item\n";
            break;
    case 4: cout << "You chose to quit\n";
        return 0;
            break;
    default: cout << "You did not enter 1, 2, 3, or 4 from the menu choices provided\n";


    }

    system("pause");
    return 0;

}
问题分析与解决

核心问题出在输入验证的条件判断逻辑上:

  • 你定义了string north;、south、east、west四个字符串变量,但从未给它们赋值,这些变量的初始值是空字符串。
  • 你的while循环判断条件是direction != north && direction != south && direction != east && direction != west,本质是在判断用户输入是否不等于空字符串——只要用户输入了非空内容,这个条件都会为真。但这完全不符合你“验证输入是否为有效方向”的需求,你真正要做的是判断输入是否不是"north"、"south"、"east"、"west"这四个字面量。

修复方案

  1. 删除冗余的north、south、east、west字符串变量,它们没有实际作用。
  2. 将while循环的判断条件改为直接与有效方向的字符串字面量比较:
while (direction != "north" && direction != "south" && direction != "east" && direction != "west")

修改后,当用户输入的方向不在有效列表中时,就会触发“Invalid entry”提示,直到输入正确方向为止。另外,你后续逻辑中已经在使用direction == "north"这类字面量比较,说明你本来就应该用这种方式判断有效方向,之前的变量是多余且错误的。

内容的提问来源于stack exchange,提问作者Brooke M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:54:35