在我的程序中,我希望用户在两种选择中进行选择。简单地说,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,请帮我解决这个问题。一定会很感激。
答案 0 :(得分:2)
试试这个:
#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或输入&lt; 0
。输入如1abc
答案 1 :(得分:1)
你也可以这样做
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";
}
}
答案 2 :(得分:0)
尝试使用std::string
阅读std::getline
,然后只需将字符串与&#34; 1&#34;进行比较或&#34; 2&#34;。否则,您可以使用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!";
}
答案 3 :(得分:0)
如果您更改程序以便cin
将用户的答案放入std :: string,那么您可以对字符串值进行测试。
如果字符串的长度()大于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 ++ -Wall test.cpp
罗伯特@ debian:/ tmp $ ./a.out 123 价值是123 值的长度为3 罗伯特@ debian:/ tmp $ ./a.out ABC 价值是abc 值的长度是3 您没有输入数字答案 4 :(得分:0)
#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)
}