C++ C++;如何防止无效输入?
在我的程序中,我希望用户在两种选择中进行选择。简单地说,1或2。我已经将程序编写到可以防止无效数值(如3或28)的位置,但我无法防止字母输入 代码如下:C++ C++;如何防止无效输入?,c++,validation,input,C++,Validation,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
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";
}
}
int而int=0;
int choiceOne_One=0;
whileInt(whileInt==0)
{
cin>>选择一个;
如果(选择一个!=1和选择一个!=2)
{
您可以尝试使用std::getline
读取std::string
,然后简单地将字符串与“1”或“2”进行比较。否则,您可以使用std::getline
读取整行,然后使用std::stoi
(C++11)将读取的字符串转换为整数。的结果告诉您转换是否成功(即字符串是否表示整数)。如果成功,则只需将整数与1
或2
进行比较。如果不成功,则引发std::invalid_参数
或std::out_of_range
异常
第一种情况(与std::string
比较):
尝试用std::getline
读取std::string
,然后简单地将字符串与“1”或“2”进行比较。否则,您可以用std::getline
读取整行,然后使用std::stoi
(C++11)将读取的字符串转换为整数。的结果告诉您转换是否成功(即字符串是否表示整数)。如果成功,则只需将整数与1
或2
进行比较。如果不成功,则引发std::invalid_参数
或std::out_of_range
异常
第一种情况(与std::string
比较):
尝试用std::getline
读取std::string
,然后简单地将字符串与“1”或“2”进行比较。否则,您可以用std::getline
读取整行,然后使用std::stoi
(C++11)将读取的字符串转换为整数。的结果告诉您转换是否成功(即字符串是否表示整数)。如果成功,则只需将整数与1
或2
进行比较。如果不成功,则引发std::invalid_参数
或std::out_of_range
异常
第一种情况(与std::string
比较):
尝试用std::getline
读取std::string
,然后简单地将字符串与“1”或“2”进行比较。否则,您可以用std::getline
读取整行,然后使用std::stoi
(C++11)将读取的字符串转换为整数。的结果告诉您转换是否成功(即字符串是否表示整数)。如果成功,则只需将整数与1
或2
进行比较。如果不成功,则引发std::invalid_参数
或std::out_of_range
异常
第一种情况(与std::string
比较):
试试这个:
#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;
}
#包括
使用名称空间std;
int main()
{
字符输入;
cin>>输入;
开关(输入)
{
案例“0”:
案例“1”:
案例“2”:
案例“3”:
案例“4”:
案例“5”:
案例“6”:
案例“7”:
案例“8”:
案例“9”:
试试这个:
#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;
}
#包括
使用名称空间std;
int main()
{
字符输入;
cin>>输入;
开关(输入)
{
案例“0”:
案例“1”:
案例“2”:
案例“3”:
案例“4”:
案例“5”:
案例“6”:
案例“7”:
案例“8”:
案例“9”:
试试这个:
#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;
}
#包括
使用名称空间std;
int main()
{
字符输入;
cin>>输入;
开关(输入)
{
案例“0”:
案例“1”:
案例“2”:
案例“3”:
案例“4”:
案例“5”:
案例“6”:
案例“7”:
案例“8”:
案例“9”:
试试这个:
#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;
}
#包括
使用名称空间std;
int main()
{
字符输入;
cin>>输入;
开关(输入)
{
案例“0”:
案例“1”:
案例“2”:
案例“3”:
案例“4”:
案例“5”:
案例“6”:
案例“7”:
案例“8”:
案例“9”:
你也可以这样做吗
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";
}
}
int而int=0;
int choiceOne_One=0;
whileInt(whileInt==0)
{
cin>>选择一个;
if(choiceOne_One==1 | | choiceOne_One==2){
如果(choiceOne_One==1){
whileInt++;
你也可以这样做吗
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";
}
}
int而int=0;
int choiceOne_One=0;
whileInt(whileInt==0)
{
cin>>选择一个;
if(choiceOne_One==1 | | choiceOne_One==2){
如果(choiceOne_One==1){
whileInt++;
你也可以这样做吗
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";
}
}
int而int=0;
int choiceOne_One=0;
whileInt(whileInt==0)
{
cin>>选择一个;
if(choiceOne_One==1 | | choiceOne_One==2){
如果(choiceOne_One==1){
whileInt++;
你也可以这样做吗
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";
}
}
int而int=0;
int choiceOne_One=0;
whileInt(whileInt==0)
{
cin>>选择一个;
if(choiceOne_One==1 | | choiceOne_One==2){
如果(choiceOne_One==1){
whileInt++;
cout如果更改程序,使cin
将用户的答案放入std::string,则可以对字符串值进行测试
如果字符串的长度()大于1,则不能为“1”或“2”
您还可以进行其他测试,如std::isalpha(int-ch)
#包括
#包括
#包括
int main(int argc,