C++ 如何防止无效输入?
C++ How to protect against invalid input?
在我的程序中,我希望用户在两个选项之间进行选择。简单地说,1 或 2。我已经编写了程序,它可以防止无效的数值,例如 3 或 28,但我无法防止按字母顺序输入。
代码如下:
int whileInt = 0;
int choiceOne_One = 0;
while(whileInt == 0)
{
cin >> choiceOne_One;
if(choiceOne_One != 1 && choiceOne_One != 2)
{
cout << "Woah! That wasn't an option! Try Again.n";
}
else if(choiceOne_One == 1)
{
whileInt++;
cout << "nYou have died. Be more careful.n";
}
else if(choiceOne_One == 2)
{
whileInt++;
cout << "nYou have survived the alien invasion due to your cunning sense of "
"safety.nCongratulations.n";
}
}
我的大脑仍然在Java中工作,请帮助我解决这个问题。一定会不胜感激。
试试这个:
#include <iostream>
using namespace std;
int main()
{
char input;
cin>>input;
switch(input)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cout<<"valid input"<<endl;
break;
default :
cout<<"Invalid input"<<endl;
}
return 0;
}
您可以为所有有效输入定义一个案例,其余部分保留为默认值。
0-9 之间的整数的 ASCII 值分别为 48-57。
如果您有输入>9 或输入<0
,则
此解决方案将没有帮助。 输入,例如 1ABC
可能有点晚了,但我最喜欢的方式,这就是我使用的:
#include <iostream>
#include <string>
#include <limits>
//T is for our variable and lambdaFunc will be our lambda function or
//function pointer
template<typename T, typename lambdaFunc>
auto secureEntry(T& variable, LambdaFunc lFunc, std::string errorMessage) //can ommit error message
{
while(!(std::cin(variable)) && !lFunc(variable)){
std::cout << errorMessage << std::endl;
std::cin.clear();
std::ignore((std::numeric_limits<std::streamsize>::max)(), 'n');
}
}
int main(int argc, char* argv[]){
int mynumber = {0};
std::string errorMessage = "Please use a number bigger than 0 and lower than 5"
secureEntry(myNumber, [](int& mynumber) -> bool{
return mynumber > 0 && mynumber < 5;
}, errorMessage)
}
你也可以这样做
int whileInt=0;
int choiceOne_One = 0;
while(whileInt == 0)
{
cin >> choiceOne_One;
if (choiceOne_One == 1 || choiceOne_One == 2){
if (choiceOne_One == 1){
whileInt++;
cout << "nYou have died. Be more careful.n";
}
else {
whileInt++;
cout << "nYou have survived the alien invasion due to your cunning sense of "
"safety.nCongratulations.n";
}
}
else {
cout << "Woah! That wasn't an option! Try Again.n";
}
}
尝试用std::getline
读取std::string
,然后简单地将字符串与"1"或"2"进行比较。否则,您可以只读取整行std::getline
,然后使用std::stoi
(C++11) 将读取字符串转换为整数。std::stoi
的结果告诉您转换是否成功(即,字符串是否表示整数)。如果成功,那么您可以简单地将整数与1
或2
进行比较。如果不成功,则会引发std::invalid_argument
或std::out_of_range
异常。
第一种情况(std::string
比较):
#include <iostream>
int main()
{
std::string input;
std::getline(std::cin, input);
if (input != "1" && input != "2")
std::cout << "Invalid input!";
}
第二种情况(使用std::stoi
):
#include <iostream>
int main()
{
std::string input;
std::getline(std::cin, input);
int n = std::stoi(input); // throws exception if cannot convert to int
if (n != 1 && n != 2)
std::cout << "Invalid input!";
}
如果您更改程序,以便cin
将用户的答案放入 std::string,那么您可以对字符串值进行测试。
如果字符串的 length() 大于 1,则不能为"1"或"2">
您还可以执行其他测试,例如std::isalpha(int ch)
#include <stdio.h>
#include <string>
#include <iostream>
int main(int argc, char * argv[]) {
std::string value;
std::cin >> value;
std::cout << "Value is " << value << "n";
std::cout << "length of value is " << value.length() << "n";
char ch;
ch = value.at(0);
if(std::isalpha(ch)) std::cout << "You did not enter a number" << "n";
return 0;
}
robert@debian:/tmp$ g++ -墙测试.cpp
robert@debian:/tmp$ ./a.out 123 值为 123 值的长度为 3
robert@debian:/tmp$ ./a.out 美国广播公司 值为abc 值的长度为 3 您没有输入数字
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 为 c++ cin 上的输入数无效生成错误消息
- 如何定义此"if block"中其他无效输入的值,以便在c ++中将字符串转换为对象?
- 处理输入流运算符>>中的无效输入
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++ QTextDocument::contentChange() 信号提供无效的输入数据
- C++循环:如何让程序在用户输入无效输入时多次重复"error"消息
- C++,程序输出无效输入,但继续通过循环
- 如何使用ISTream对象输入多个字符到对象的数组中?(and 0作为哨兵值.下面的我的代码将无效)
- 当我输入任何内容时,我的代码输出无效
- 如果用户输入无效,则循环
- 如果用户只按Enter或输入无效,则C 如何重新启动循环
- 流中的输入无效C++
- 程序工作,但当输入无效时,它会继续循环 c++
- 如何停止由于输入C++无效而导致的无限循环
- 当用户输入无效整数时,c++菜单卡住了
- dll 中的输入无效<dllname.dll>但我<functionname>没有看到错误