gpt4 book ai didi

c++ - 用户输入...如何检查 ENTER 键

转载 作者:行者123 更新时间:2023-11-30 01:00:26 25 4
gpt4 key购买 nike

我有一段代码,用户从键盘输入。我想在按下 ENTER 时做一些事情。我正在检查 '\n' 但它不起作用。如何检查用户是否按下了 ENTER 键?

if( shuffle == false ){
int i=0;
string line;
while( i<20){
cout << "Playing: ";
songs[i]->printSong();
cout << "Press ENTER to stop or play next song: ";
getline(cin, line);
if( line.compare("\n") == 0 ){
i++;
}
}
}

最佳答案

if( shuffle == false ){
int i=0;
string line;
while( i<20){
cout << "Playing: ";
songs[i]->printSong();
cout << "Press ENTER to stop or play next song: ";
if( cin.get() == '\n' ) {
i++;
}
}
}

关于c++ - 用户输入...如何检查 ENTER 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2688283/

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