English 中文(简体)
我如何使用 cin 执行单行整数命令行类型输入验证?
原标题:How do I implement single line integer type command line input validation using cin?
  • 时间:2012-05-22 04:42:46
  •  标签:
  • c++

我有一个程序要求用户输入范围 [0,2] 的整数。 我使用以下链接作为向导 。

< a href=" "https://stackoverflow.com/ questions/2075898/good-input-validation-loop-using-c" > 使用 cin - C/a > 的好输入验证环

但是,当用户按键输入时不输入数据,光标只是进入命令提示中的下一行,而我更希望它能促使用户输入一个有效号码。在这种情况下,提醒用户是有道理的,还是有理由不作为单行输入执行验证?如果是字符串,我会使用“获取线”来解决这个问题,我是否应该在此情况下使用这个代码?这是我基于上述链接的代码:

#include <iostream>

int main()
{
    int answeredNumber;
    while(1)
    {
        std::cout << "Enter your answer: ";
        if(std::cin >> answeredNumber && answeredNumber >= 0 && answeredNumber <= 2)
        {
            break;
        }
        else
        {
            std::cout << "Please enter a valid answer: " ;
            std::cin.clear();
          std::cin.ignore(std::numeric_limits<std::streamsize>::max(),  
 );
        }
    }
    system("pause");
}
最佳回答

s 因为获得一个有 cin 的整数将会跳过主白空格, 包括 一条新线。 围绕这条线没有容易的方法 。

如果您想要基于行的输入, 您可以以 < em> string 的形式获得输入值, 然后解释 :

#include <iostream>
#include <sstream>
#include <string>

int main (void) {
    std::string inputLine;
    int answer;
    std::cout << "Enter your answer: ";
    while(1) {
        getline (std::cin, inputLine);
        std::stringstream ss (inputLine);
        if ((ss >> answer))
            if ((answer >= 0) && (answer <= 2))
                break;
        std::cout << "No, please enter a VALID answer: " ;
    }
    std::cout << "You entered " << answer <<  
 ;
    return 0;
}
问题回答

另一个答案指定了问题。然而,对您的代码稍作修改,我想可以解决您眼前的问题:

int answeredNumber = std::cin.get() -  0 ;
if(answeredNumber >= 0 && answeredNumber <= 2)

std::cin 与 & gt; & gt; 操作员接收 < em> formated 数据, 您需要使用 < code> get () 和其他功能接收 < em > unformated 数据 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签