繁体   English   中英

如何在 C++ 中限制用户输入字符串和字符?

[英]How to limit user input in C++ for strings & characters?

我正在尝试创建一个小型餐厅程序,我将在其中练习到目前为止我在 C++ 中学到的所有内容。 但是我跳进了一个小问题。 在程序开始时,我提示用户是否要进入程序,或者选择Y或N退出程序。如果输入的不是其他任何内容,程序会告诉用户无效。

问题是假设用户输入了一个无效字符 a。 无效的 output 将正常显示,一切看起来都很完美。 但如果用户输入两个或更多字符,则无效的 output 案例将被打印与用户输入的字符一样多。 下面的示例:

Output 图像

#include <iostream>

int main()
{
    char ContinueAnswer;
    std::string Employee {"Lara"};
    std::cout << "\n\t\t\t---------------------------------------"
              << "\n\t\t\t|                                     |"    
              << "\n\t\t\t|            Welcome to OP            |"
              << "\n\t\t\t|Home to the best fast food in Orlando|"
              << "\n\t\t\t|                                     |"
              << "\n\t\t\t--------------------------------------|" << std::endl;

do
{
    std::cout << "\n\t\t\t    Would you like to enter? (Y/N)"
              << "\n\t\t\t                  "; std::cin >> ContinueAnswer;
    if(ContinueAnswer == 'y' || ContinueAnswer == 'Y')
    {
        system("cls");
        std::cout << "\n\t\t\t              My name is " << Employee << "."
                  << "\n\t\t\tI will assist you as we go through the menu." << std::endl;
    }
    else if(ContinueAnswer == 'n' || ContinueAnswer == 'N')
    {
        std::cout << "\t\t\t\tGoodbye and come again!" << std::endl;
        return 0;
    }
    else
        std::cout << "\n\t\t\t\t  Invalid Response" << std::endl;
}
while(ContinueAnswer != 'y' && ContinueAnswer != 'Y')

感谢您花时间阅读并感谢任何回答的人:)

您可以简单地让用户输入一个string

std::string ContinueAnswer;

并像这样比较:

if(ContinueAnswer == "y" || ContinueAnswer == "Y")

它将处理多字符输入。

如果您还想处理输入中的空格,请更改:

std::cin >> ContinueAnswer;

至:

std::getline(std::cin, ContinueAnswer);

在解决您的问题之前,我需要指出,在对输入进行任何操作之前,您应该始终验证输入是否成功。 由于 inout 失败而未设置的处理变量是一个相当常见的错误来源。 例如:

if (std::cin >> ContinueAnswer) {
    // do something with successfully read data
}
else {
    // deal with the input failing, e.g., bail out
}

如果读取了九个预期字符,我假设您认为同一行上的所有内容都是无效的。 可以将一行读入std::string 但是,这可能会被滥用以提供极长的输入行,最终会使您的程序崩溃。 此外,将数据读入std::string只是为了将其丢弃似乎是不明智的。 我建议忽略所有字符,包括可以使用的换行符(您需要包含<limits>用于这种方法):

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);

第一个参数是一个特殊值,表示在换行符之前可以有任意数量的字符。 在实践中,您可能可以使用像 1000 这样的值,这很好,但它可以被玩弄。 当然,在实际应用中,可以使用专用限制来防止对手长时间保持程序繁忙。 我倾向于假设我的程序受到攻击,以确保我处理不寻常的情况。

快速重构会产生以下结果:

#include <iostream>
#include <cstring>
#include <stdio.h>

int main()
{
        char ContinueAnswer[256];
        std::string Employee {"Lara"};
        std::cout << "\n\t\t\t---------------------------------------"
              << "\n\t\t\t|                                     |"
              << "\n\t\t\t|            Welcome to OP            |"
              << "\n\t\t\t|Home to the best fast food in Orlando|"
              << "\n\t\t\t|                                     |"
              << "\n\t\t\t--------------------------------------|" << std::endl;

        do
        {
            std::cout << "\n\t\t\t    Would you like to enter? (Y/N)"
                      << "\n\t\t\t                  "; std::cin.getline(ContinueAnswer,sizeof(ContinueAnswer));
            if(strcmp(ContinueAnswer, "Y") == 0 || strcmp(ContinueAnswer, "y") == 0)
            {
                system("cls");
                std::cout << "\n\t\t\t              My name is " << Employee << "."
                          << "\n\t\t\tI will assist you as we go through the menu." << std::endl;
            }
            else if(strcmp(ContinueAnswer, "N") == 0 || strcmp(ContinueAnswer, "n") == 0)
            {
                std::cout << "\t\t\t\tGoodbye and come again!" << std::endl;
                return 0;
            }
            else
                std::cout << "\n\t\t\t\t  Invalid Response" << std::endl; 
        }       
        while(true);
}

cin.getline 将获取所有字符,直到一个分隔符。 然后,您可以使用 strcmp 检查等效性并拒绝除您想要的以外的任何内容。 最后,您似乎希望它处于无限循环中,所以不要担心最后检查输入并返回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM