gpt4 book ai didi

c++ - 在使用 cin 请求 int 时如何有效地防止用户输入?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:04:53 24 4
gpt4 key购买 nike

这是我在这里发表的第一篇文章,我是编程/C++ 的新手(实际上才进入这个兔子洞几周)。我在 Microsoft Visual Studio 2017 中有这个测试项目,我正试图弄清楚如何在使用 cin 时完全防止用户输入。如果我要求一个 int,而我只想要一个 1 或一个 0,我希望绝对没有办法让某人在诸如 2 或 n 之类的东西中放置,或者在不正确和正确的响应之间放置空格,例如“n 1”。目前,我已经到了这样的地步,我似乎可以创建一个不需要的结果的唯一方法是,如果我首先输入一个正确的整数(0 或 1),然后在后面跟一个空格和任何其他字符,这个模式可以在初始正确的字符(0 a 42 f 9130)等之后跟上无限数量的空格和字符。除了用更困惑的代码获得所需的结果之外,我想知道我是否只是遗漏了一些 -我还没有听说过的内置功能可以使这个过程更加高效。这是我为达到这一点而写的:

#include <iostream>
#include <string>
#include <climits>

using namespace std;

int trap;
int wrongNumber();

int numOnly() {
while (cin.fail())
{
// Using someone else's code for this while statement to figure out how to not take a char input when using an int
// Update: Turned this into a function to be called on whenever cin has a chance to fail because users don't listen.

cin.clear(); // clear input buffer to restore cin to a usable state
cin.ignore(INT_MAX, '\n'); // ignore last input
system("CLS");
cout << "----------------------------------" << endl;
cout << "| You can only enter a number. |" << endl;
cout << "| Would you like to pick a card? |" << endl;
cout << "| Type 1 for yes or 0 for no! |" << endl;
cout << "----------------------------------" << endl;
cin >> trap;
}
if (trap != 1 && trap != 0) {
system("CLS");
wrongNumber();
}
return trap;
}

int wrongNumber() {

// At least I made this fail-safe on my own!

while (trap != 1 && trap != 0) {
system("CLS");
cout << "----------------------------------" << endl;
cout << "| That is not a 1 or a 0! |" << endl;
cout << "| Would you like to pick a card? |" << endl;
cout << "| Type 1 for yes or 0 for no! |" << endl;
cout << "----------------------------------" << endl;
cin >> trap;
}
if (cin.fail()) {
system("CLS");
numOnly();
}
return trap;
}

int main() {

cout << "----------------------------------" << endl;
cout << "| Would you like to pick a card? |" << endl;
cout << "| Type 1 for yes or 0 for no! |" << endl;
cout << "----------------------------------" << endl;
cin >> trap;

while (cin.fail())
{
numOnly();
}

if (trap != 1 && trap != 0) {
wrongNumber();
}

最佳答案

我建议您不要使用整数来存储"is"或“否”的答案,而是使用字符串。通过这种方式,您可以使用 cin.fail()cin.ignore()cin.clear() 为自己节省一些代码行:

 int main() {

string trap;
cout << "----------------------------------" << endl;
cout << "| Would you like to pick a card? |" << endl;
cout << "| Type 1 for yes or 0 for no! |" << endl;
cout << "----------------------------------" << endl;
cin>>trap;

while (trap != "1" && trap != "0") { //You can make this while block into a function if you prefer
cout << "----------------------------------" << endl;
cout << "| That is not a 1 or a 0! |" << endl;
cout << "| Would you like to pick a card? |" << endl;
cout << "| Type 1 for yes or 0 for no! |" << endl;
cout << "----------------------------------" << endl;
cin>>trap;
}
return 0;
}

如果您必须使用整数,那么您应该查看thiscapturing characters without pressing enter .

关于c++ - 在使用 cin 请求 int 时如何有效地防止用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52285892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com