如何检查用户输入是 1 还是 2
How to check if user input is a 1 or a 2
我试图要求用户输入一个表示难度的数字,但是当我使用if
语句检查难度是否为 1 时,尽管我输入了1
,但if
中的代码块从未执行。
与问题的答案相同。
例如,当我运行代码时,它要求我输入难度,我输入 2 或 1000,它仍然继续执行代码。答案也是如此,即使我输入 1945 作为第一个错误的问题,它也会显示you got it correct
。
任何帮助将不胜感激!
法典:
int difficulty;
cout << "Please choose a difficulty: 1 for easy 2 for medium 3 for hard: ";
cin >> difficulty;
if (difficulty = '1')
{
ifstream file_("questions.txt");
if (file_.is_open())
{
std::getline(file_, ques1);
cout << ques1 << std::endl;
int answr1;
cout << "Enter 1 for: 1945, Enter 2 for: 1914 n";
cin >> answr1;
if (answr1 = '1914')
{
cout << "Well done you got it correct! n";
}
else {
cout << "Incorrect answer was 1914! n";
}
std::getline(file_, ques2);
cout << ques2 << std::endl;
int answr2;
cout << "Enter 1 for: 1937, Enter 2 for: 1939 n";
cin >> answr2;
if (answr2 = '1939')
{
cout << "Well done you got it correct! n";
}
else {
cout << "The answer was 1939 n";
}
std::getline(file_, ques3);
cout << ques3 << std::endl;
int answr3;
cout << "Enter 1 for: George Bush, Enter 2 for: George Washington n";
cin >> answr3;
if (answr3 = '2')
{
cout << "Well done you got it correct! n";
}
else {
cout << "It was George Washington n";
}
std::getline(file_, ques4);
cout << ques4 << std::endl;
int answr4;
cout << "Enter 1 for: 14, Enter 2 for: 11 n";
cin >> answr4;
if (answr3 = '11')
{
cout << "Well done you got it correct! n";
}
else {
cout << "It was 11 n";
}
std::getline(file_, ques5);
cout << ques5 << std::endl;
int answr5;
cout << "Enter 1 for: She was born with 6 figners , Enter 2 for: 6 toes n";
cin >> answr5;
if (answr3 =' 2')
{
cout << "Well done you got it correct! n";
}
else {
cout << "It was 6 Toes n";
}
file_.close();
}
else {
cout << "File is not openn";
}
}
首先(建议!(,您正在处理用户输入。您应该始终考虑用户输入不正确的可能性!
int n;
std::cin >> n;
如果用户键入例如"x",则流将进入无效状态,您将无法再成功读取其他任何内容。因此,在每次输入后,您应该检查流:
if(!std::cin)
{
// print some error message?
// reset the error state!
std::cin::clear();
// skip whatever yet is buffered in the stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max())
// now std::cin is ready to use again...
}
您可以在循环中执行此操作,对输入进行进一步检查,并且仅在有效时才退出......尝试制作一个函数以避免代码重复。
int difficulty;
std::cin >> difficulty;
if (difficulty = '1') // << !!!
同时出现两个错误:单个等号不是比较,而是作业!因此,您将字符"1"的值(在数字上不是 1!(分配给变量difficulty
– 然后检查此赋值的结果。由于变量现在获得的值不同于 0(零(,因此将始终输入 if块。为了进行比较,您需要使用:
if(difficulty == '1')
// ^^
好的,保留值:'1'
是字符文字,而不是数字文字。最有可能的是,您在前 128 个字符上使用的字符集与 ASCII 兼容,如果是,则'1'
对应于数值 49。很确定这不是你想要的...所以:
if(difficulty == 1)
// ^ integer literal, numerical value!
或者,您可以阅读一个字符:
char difficulty; // different type!
std::cin >> difficulty;
if (difficulty = '1')
现在,这将读取一个简单的字符,根本不进行任何转换,而读取 int 将一个或多个(!(字符转换为数值。如果不进行转换,字符输入将保持原样,您可以与字符值进行比较...
int answr1;
cin >> answr1;
if (answr1 = '1914')
同样,但是,现在您使用的是无效字符文字!
您可以与字符串进行比较,如果有的话:
std::string answr1;
cin >> answr1;
if (answr1 == "1914")
或再次作为整数,如上(if(a == 1914)
(。但另一方面:
std::cout << "Enter 1 for: 1945, Enter 2 for: 1914 n";
上面的比较要求用户不要输入正确答案的2
,而是1914
!因此,您宁愿与选项进行比较:
if(answr1 == 2)
最后,一些进一步的建议:您将文本文件中的问题与代码中的静态链接响应选项相结合。好吧,您的文本文件以这种方式是徒劳的...
您也可以尝试从文件中拖动答案。进一步的优点:您可以通过使用循环来避免重复代码,并且可以在不同的文件中组织针对不同难度的问题。然后,您的代码可能类似于以下代码:
std::ifstream file;
std::cin >> difficulty;
switch(difficulty)
{
case 1:
file.open("question_1.txt");
break;
case 2:
case 3:
// ...
break;
default:
// invalid input! -> appropriate handling
break;
}
std::string text;
for(;;)
{
std::getline(file, text);
// the question text:
std::cout << text << std::endl;
// the answer options:
std::getline(file, text);
std::cout << text << std::endl;
// read the correct answer:
std::getline(file, text);
int answer; // parse appropriately from text!
// read the text for output, if users answer was not correct!
std::getline(file, text);
// now read in user input and compare against correct answer...
}
文件格式现在非常明显:每个问题四行,内容如上所述。你可能会发明一些更复杂的东西,甚至使用XML,但现在,它应该足够了......
好吧,您仍然需要正确确定文件的结尾并在到达时退出循环,将其留给您作为练习......
要进行相等比较,请使用:==
:
if(difficulty==1)
{
cout << "You selected difficulty 1" << std::endl;
}
如果你这样做
if(difficulty=1)
{
cout << "You selected difficulty 1" << std::endl;
}
1 分配给难度。检查if
条件(在这种情况下,检查变量不为 0(,因此始终满足。
你的逻辑在这里有缺陷,你的if语句。您希望用户在两个选项之间进行选择,因此,如果他们选择 1 或 2 以外的任何选项,请给他们提示并期望这两个答案之一。请参阅以下示例:
if (answr1 == "1914")
{
cout << "Well done you got it correct! n";
}
else if(answr1 == '1945') {
cout << "Incorrect answer was 1914! n";
}
else {
cout << "Incorrect Input!Plese input 1 or 2 n";
}
PS:在C++=
运算符用于分配。使用==
进行比较
- enum是C++中的宏变量还是整数变量
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 我已经创建了一个模板函数,但是在检查输入是奇数还是偶数时收到错误 C++.
- 如何检查用户输入是 1 还是 2
- 输入正整数或负整数列表,以使用开关函数 c++ 确定数字是偶数还是奇数
- 检测输入是字符串还是整数
- 如何识别输入的输入字符串在 c++ 中是两个还是多个单词
- 我应该使用 DeviceContext 函数在管道中循环输入还是通过创建来输入
- 如何检查输入是整数,字符串还是浮动在C 中
- 如何测试 Windows 中的触摸输入是来自手指还是触笔?在 C/C++ 中
- 如何测试输入是字符串还是int
- 使用 stringstream.fail() 检查输入是 int 还是 string
- 如何检查用户CIN输入是太少还是太多
- 使用整型变量检查用户输入是整型还是字符串
- 接受用户输入,无论他们在c++中输入大写还是小写
- 我可以检查输入的值是字符串还是整数在c++
- 如何检查单个字符串输入的部分是int还是char