C++检测用户按下的ENTER键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2803502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Detecting ENTER key pressed by user
提问by user69514
I have a loop where I ask the user to enter a name. I need to stop when the user presses the ENTER key..... or when 20 names have been entered. However my method doesn't stop when the user presses the ENTER key
我有一个循环,我要求用户输入名称。当用户按下 ENTER 键..... 或输入 20 个名称时,我需要停止。但是,当用户按下 ENTER 键时,我的方法不会停止
//loop until ENTER key is entered or 20 elements have been added
bool stop = false;
int ind = 0;
while( !stop || ind >= 20 ){
cout << "Enter name #" << (ind+1) << ":";
string temp;
getline(cin, temp);
int enterKey = atoi(temp.c_str());
if(enterKey == '\n'){
stop = true;
}
else{
names[ind] = temp;
}
ind++;
}
回答by sth
You convert the read string to an integer with atoi
:
您可以使用以下命令将读取的字符串转换为整数atoi
:
int enterKey = atoi(temp.c_str());
If temp is a string like "1234"
this will set enterKey
to 1234
. Then you compare enterKey
to the ASCII value of \n
. This is most probably not doing anything useful.
如果 temp 是这样的字符串,"1234"
则将设置enterKey
为1234
. 然后enterKey
与 的 ASCII 值进行比较\n
。这很可能没有做任何有用的事情。
Also std::getline
just read the characters up to, but not including, the next '\n'
. If a user just presses enter without typing any other characters, std::getline
will return an empty string. If a string is empty can be easily tested with its empty()
method:
也std::getline
只需读取字符,但不包括下一个'\n'
. 如果用户只按 Enter 键而不输入任何其他字符,std::getline
将返回一个空字符串。如果字符串为空,可以使用其empty()
方法轻松测试:
getline(cin, temp);
if (temp.empty()) {
stop = true;
}
回答by Charles Beattie
try:
尝试:
while( !stop && ind < 20 )
or:
或者:
using namespace std;
vector <string> names; // edited.
for (int ind = 0; ind < 20; ++ind)
{
cout << "Enter name #" << (ind+1) << ":";
string temp;
getline(cin, temp);
if (temp.empty())
break;
names.push_back(temp);
}
回答by LukeN
getline will eat your delimiter, which will be '\n', so you probably want to be checking for an empty string. Do it before the call to atoi.
getline 会吃掉你的分隔符,这将是 '\n',所以你可能想要检查一个空字符串。在调用 atoi 之前执行此操作。
回答by Geoff
Try stop = temp.empty()
instead. getline
should not contain any new-line characters. An empty line should result in an empty string.
试试吧stop = temp.empty()
。getline
不应包含任何换行符。空行应产生空字符串。
Also, Charles is correct, your while condition is incorrect, use while( !stop && ind < 20)
. The way you have it written the user needs to enter 20 values, and an empty line. Charles' change says to break when either condition is met (not both).
另外,Charles 是正确的,您的 while 条件不正确,请使用while( !stop && ind < 20)
. 您编写它的方式是用户需要输入 20 个值和一个空行。Charles 的更改表示在满足任一条件(不是同时满足)时中断。
For the sake of completeness, here's the proposed new code:
为了完整起见,这里是建议的新代码:
bool stop = false;
int ind = 0;
while( !stop && ind < 20 ){
cout << "Enter name #" << (ind+1) << ":";
string temp;
getline(cin, temp);
if(temp.empty()) {
stop = true;
} else {
names[ind] = temp;
}
ind++;
}
Personally, I would write the code as follows:
就个人而言,我会编写如下代码:
vector<string> names;
for(int ind = 0; ind < 20; ind++) {
cout << "Enter name #" << (ind + 1) << " (blank to stop): ";
string name;
getline(cin, name);
if(name.empty() || cin.eof()) {
break;
}
names.push_back(name);
}
cout << "Read " << names.length() << " names before empty line detected." << endl;
回答by Puppy
You want to use cin.get(); cin >> temp; I do believe.
你想使用 cin.get(); cin >> 温度;我相信。