C+中的整数输入验证+; 我有一个关于C++中输入验证的问题。以下是我遇到问题的代码: #include <iostream> using namespace std; int main() { int in; while(true) { if(cin >> in) { if(in < 0 || in > 2) { cout << "Invalid input. Number needs to be 0, 1, or 2."; cin.clear(); while(cin.get() != '\n'); cin.ignore(); } else { cout << "Output: " << in; break; } } else { cout << "Invalid input. Please enter a number."; cin.clear(); while(cin.get() != '\n'); cin.ignore(); } } } #包括 使用名称空间std; int main() { int-in; while(true) { 如果(cin>>输入) { 如果(in2) { cout
主要问题是,当使用C+中的整数输入验证+; 我有一个关于C++中输入验证的问题。以下是我遇到问题的代码: #include <iostream> using namespace std; int main() { int in; while(true) { if(cin >> in) { if(in < 0 || in > 2) { cout << "Invalid input. Number needs to be 0, 1, or 2."; cin.clear(); while(cin.get() != '\n'); cin.ignore(); } else { cout << "Output: " << in; break; } } else { cout << "Invalid input. Please enter a number."; cin.clear(); while(cin.get() != '\n'); cin.ignore(); } } } #包括 使用名称空间std; int main() { int-in; while(true) { 如果(cin>>输入) { 如果(in2) { cout,c++,C++,主要问题是,当使用>运算符从std::cin请求int时,将转换从输入开始的一系列数字字符。示例: 2将转换为2 75$将转换为75 12asdfgh将转换为12 hello,world将转换为0,因为第一个字符已经不是数字 最好使用一些char操作: int getNumber() { char input; std::cin >> input; if(input > '2' || input < '0') { // yes, chars act lik
>
运算符从std::cin
请求int
时,将转换从输入开始的一系列数字字符。示例:
将转换为2
2
将转换为75$
75
将转换为12asdfgh
12
将转换为hello,world
,因为第一个字符已经不是数字0
char
操作:
int getNumber() {
char input;
std::cin >> input;
if(input > '2' || input < '0') { // yes, chars act like ASCII numbers
// handle this problem
return -1; //error
} else {
return int(input - '0'); // input's code - '0''s code = number
}
}
int getNumber(){
字符输入;
std::cin>>输入;
如果(输入>'2'| |输入<'0'){//yes,字符的行为类似于ASCII数字
//处理这个问题
返回-1;//错误
}否则{
返回int(输入-'0');//输入的代码-'0'的代码=数字
}
}
主要问题是,当使用>
运算符从std::cin
请求int
时,将转换输入开头的一系列数字字符。示例:
将转换为2
2
将转换为75$
75
将转换为12asdfgh
12
将转换为hello,world
,因为第一个字符已经不是数字0
char
操作:
int getNumber() {
char input;
std::cin >> input;
if(input > '2' || input < '0') { // yes, chars act like ASCII numbers
// handle this problem
return -1; //error
} else {
return int(input - '0'); // input's code - '0''s code = number
}
}
int getNumber(){
字符输入;
std::cin>>输入;
如果(输入>'2'| |输入<'0'){//yes,字符的行为类似于ASCII数字
//处理这个问题
返回-1;//错误
}否则{
返回int(输入-'0');//输入的代码-'0'的代码=数字
}
}
在处理用户输入时,我会使用以下方法:
string l;
if(!getline(cin, l))
/* handle error that no input could be read at all */
try
{
int const res = boost::lexical_cast<int>(l);
/* numerically validate input here */
return res;
}
catch(exception const&)
{
/* handle parsing error here */
}
字符串l;
如果(!getline(cin,l))
/*处理无法读取任何输入的错误*/
尝试
{
int const res=boost::词法转换(l);
/*在此对输入进行数值验证*/
返回res;
}
捕获(异常常量&)
{
/*在这里处理解析错误*/
}
换句话说,读取一行,然后使用Boost的
lexical_cast()
函数模板对其进行解析和验证。请注意,如果有人从文件读取输入(例如通过shell重定向),则会出现第一个错误,即getline()
失败,但这也可以通过某些按键来实现,具体取决于shell。此状态无法从中恢复,因此提示不同的答案将导致无休止的循环。在处理用户输入时,我将使用以下方法:
string l;
if(!getline(cin, l))
/* handle error that no input could be read at all */
try
{
int const res = boost::lexical_cast<int>(l);
/* numerically validate input here */
return res;
}
catch(exception const&)
{
/* handle parsing error here */
}
字符串l;
如果(!getline(cin,l))
/*处理无法读取任何输入的错误*/
尝试
{
int const res=boost::词法转换(l);
/*在此对输入进行数值验证*/
返回res;
}
捕获(异常常量&)
{
/*在这里处理解析错误*/
}
换句话说,读取一行,然后使用Boost的
lexical_cast()
函数模板对其进行解析和验证。请注意,如果有人从文件读取输入(例如通过shell重定向),则会出现第一个错误,即getline()
失败,但这也可以通过某些按键来实现,具体取决于shell。此状态无法恢复,因此提示输入不同的答案将导致无休止的循环。如果您查看提取操作符的文档,例如:
您将注意到以下报价:
(1)算术类型
从流中按顺序提取和解析字符,以将其解释为正确类型值的表示形式,
它存储为val的值
这本质上意味着程序将尝试将您传递的所有数据按右值指定的格式处理,在您的例子中为int。更简单的是:在您的例子中,它将尝试将您传递到流中的内容设置为int,并将数据设置为“integerish”离子运算符,例如: 您将注意到以下报价: (1)算术类型 从流中按顺序提取和解析字符,以将其解释为正确类型值的表示形式, 它存储为val的值
这本质上意味着程序将尝试将您传递的所有数据视为以右值指定的格式格式化的数据,在您的情况下为int。更简单的是:在您的情况下,它将尝试将您传递到流中的内容设置为int,并将数据设置为“integerish”。如果您希望它介于0和2之间,则应设置为(in<0 | | in>2)你的意思是,当它输出时,当我写东西时,输出应该是0、1或2,它将结束?@dietbacon谢谢,这是一个打字错误。忽略(10000),\n);定义可能是,它读取一个数字,并在它不理解的第一个字符处停止。只有当读取的字符数为零时,它才被视为失败,否则其余的字符将保留缓冲以供下次读取操作。如果希望它介于0和2之间,则应为if(in<0 | | in>2)你的意思是,当它输出时,当我写东西时,输出应该是0、1或2,它会结束吗?@dietbacon谢谢,这是一个打字错误。忽略(10000),\n');可能定义是它读取一个数字,并在它不理解的第一个字符处停止。只有当读取的字符数为零时,它才被视为失败,否则其余的字符将保留缓冲以供下一次读取操作。“hello,world”不会转换为零,而是设置失败位。我想你的意思是
如果(输入>'2'| |输入<'0')