英文:
How to "safely" use cin with a char
问题
这似乎是一个愚蠢的问题,但我找不到关于这个的太多信息。
我的代码接受用户输入,只期望接收't'
或'f'
。
我很难"保护"程序免受边缘情况的影响(例如,如果输入包含多个字符和/或空格,它将循环遍历缓冲区中的每个char
,直到找到't'
或'f'
)。
我理解使用cin.fail()
仅适用于int
。是否有类似的东西我可以用于char
?我希望类似于这样的东西起作用。我只是很难弄清楚如何在这种情况下定义cin
的错误/失败,以及如何阻止它循环遍历整个缓冲区。
char choice = '0';
while(choice != 't' && choice != 'f') {
std::cout << "Is this answer true or false? (t/f) ";
std::cin >> choice;
if (error_with_input) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error. char not entered. Please try again";
}
}
我是不是从错误的角度来看待这个问题?我应该只是将choice
变量更改为string
而不是char
吗?然后我可以使用getline()
,测试choice
的长度是否为1,如果是,然后最终测试choice
是否为't'
或'f'
。这似乎有点低效,但也许这是一个更好的解决方案?
英文:
This seems like a dumb question, but I can't find much info about this.
My code takes user input, and only expects to receive 't'
or 'f'
.
I have a hard time "protecting" the program from edge cases (ex: if the input contains multiple characters and/or spaces, it will loop through every char
in the buffer until it finds 't'
or 'f'
).
I understand that using cin.fail()
only works for an int
. Is there something similar that I could use for a char
? I would like if something along the lines of this worked. I'm just having a hard time figuring out how to define an error/failure of cin
in this case, and how to stop it from looping through the entire buffer.
char choice = '0';
while(choice != 't' && choice != 'f') {
std::cout << "Is this answer true or false? (t/f) ";
std::cin >> choice;
if (error_with_input) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error. char not entered. Please try again";
}
}
Am I looking at this from the wrong point of view? Should I just make the choice
variable a string
instead of a char
? I could then use getline()
, test if choice
has a length of 1, and if so then finally test if choice
is either 't'
or 'f'
. It seems a little inefficient, but maybe it's a better solution?
答案1
得分: 2
读取输入作为 std::string
,或者至少作为 char[]
,这是正确的方法。在读取之后验证输入,而不是在读取时验证,例如:
std::string choice;
do {
std::cout << "这个答案是真还是假? (t/f) ";
std::cin >> choice;
if (choice != "t" && choice != "f") {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "错误。未输入字符。请重试";
}
else {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
}
}
while (true);
英文:
Reading the input as a std::string
, or at least as a char[]
, is the right way to go. Validate the input after you have read it in, not while you are reading it, eg:
string choice;
do {
std::cout << "Is this answer true or false? (t/f) ";
std::cin >> choice;
if (choice != "t" && choice != "f") {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error. char not entered. Please try again";
}
else {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
}
}
while (true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论