Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理错误的数据类型输入 在C++中,你如何处理错误的输入?例如,如果程序要求输入一个整数,当您键入一个字符时,它应该能够执行某些操作,然后循环以重复输入,但当您需要输入一个整数时,循环将无限大,反之亦然_C++_Input_Types_Error Handling - Fatal编程技术网

如何处理错误的数据类型输入 在C++中,你如何处理错误的输入?例如,如果程序要求输入一个整数,当您键入一个字符时,它应该能够执行某些操作,然后循环以重复输入,但当您需要输入一个整数时,循环将无限大,反之亦然

如何处理错误的数据类型输入 在C++中,你如何处理错误的输入?例如,如果程序要求输入一个整数,当您键入一个字符时,它应该能够执行某些操作,然后循环以重复输入,但当您需要输入一个整数时,循环将无限大,反之亦然,c++,input,types,error-handling,C++,Input,Types,Error Handling,程序进入无限循环的原因是由于输入失败而设置了std::cin的错误输入标志。要做的事情是清除该标志并从输入缓冲区中丢弃错误输入 //executes loop if the input fails (e.g., no characters were read) while (std::cout << "Enter a number" && !(std::cin >> num)) { std::cin.clear(); //clear bad inp

程序进入无限循环的原因是由于输入失败而设置了
std::cin
的错误输入标志。要做的事情是清除该标志并从输入缓冲区中丢弃错误输入

//executes loop if the input fails (e.g., no characters were read)
while (std::cout << "Enter a number" && !(std::cin >> num)) {
    std::cin.clear(); //clear bad input flag
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
    std::cout << "Invalid input; please re-enter.\n";
}
//如果输入失败(例如,未读取任何字符),则执行循环
while(std::cout>num)){
std::cin.clear();//清除错误输入标志
std::cin.ignore(std::numeric_limits::max(),'\n');//放弃输入

std::cout测试输入是否符合程序的要求。如果不是,则提醒用户他们提供的输入不可接受。

如果ASCII值在65到90或97到122之间,则可以通过ASCII值进行检查。

最受欢迎的答案很好地涵盖了解决方案

除此之外,这可能有助于更好地形象化正在发生的事情:

int main()

    int input = 1;//set to 1 for illustrative purposes
    bool cinState = false;
    string test = "\0";
    while(input != -1){//enter -1 to exit
        cout << "Please input (a) character(s): ";//input a character here as a test
        cin >> input; //attempting to input a character to an int variable will cause cin to fail
        cout << "input: " << input << endl;//input has changed from 1 to 0
        cinState = cin;//cin is in bad state, returns false
        cout << "cinState: " << cinState << endl;
        cin.clear();//bad state flag cleared
        cinState = cin;//cin now returns true and will input to a variable
        cout << "cinState: " << cinState << endl;
        cout << "Please enter character(s): ";
        cin >> test;//remaining text in buffer is dumped here. cin will not pause if there is any text left in the buffer.
        cout << "test: " << test << endl;
    }
    return 0;    
}
intmain()
int input=1;//设置为1以便于说明
bool-cinState=false;
字符串测试=“\0”;
while(输入!=-1){//输入-1退出
cout>input;//尝试向int变量输入字符将导致cin失败

在问这个问题之前,我可以做一些研究吗?我看到他们把cin.ignore(1000,\n');这是做什么的?还有!(cin>>num)返回一个布尔值?我不知道that@Marvin,
cin.ignore(1000,“\n”)
忽略/丢弃输入缓冲区中的字符,直到丢弃1000个字符或遇到换行符为止,以先到者为准。这是一种摆脱行的好方法。您将在parashift示例中看到,它们使用流的最大大小而不是1000来解释最大长度的行。我使用
cin.sync()
因为在执行此操作时,我希望与用户处于平等的地位(还没有阅读下一行)最后,
cin
有一个
操作符void*
,所以它不能转换成bool.@Marvin,
cin>>num
如果用户键入,比如在需要int时输入“a”,它就会失败。它提供了一个转换操作符,允许它隐式转换成
void*
。如果
cin
处于错误状态,它将返回
NULL
。如果不是,它将返回对象。然后可以将其转换为bool:如果不是NULL,则为true;如果是NULL,则为false。然后循环可以使用它来计算所需的bool表达式。@VaisakMohan,这也可以;这是完成最后一段的一种方法。但是,请注意是否要计算multI将输入放在一行上。如果您读取一行有效输入,转换其中的第一部分,然后丢弃其他部分,那将是一件非常遗憾的事情。
getline
最适合实际的基于行的输入,而不是基于令牌的输入。您还必须注意空格,在转换过程中可能会有不同的处理方式。除此之外,逐行读取means您必须进行转换和错误检查(减去丢弃流中的错误输入)而不仅仅是后者。此外,对于
getline
,在使用它之前,您必须小心不要在流中留下错误的换行符。如果使用
getline
,通常建议全力以赴,而不是尝试将其与基于令牌的输入混合匹配。您如何知道系统正在使用ASCII?