当用户键入分隔符时,停止getline()输入

Stop getline() input when user types delimiter character

本文关键字:getline 停止 输入 用户 分隔符      更新时间:2023-10-16

我正试图找到一种方法,当用户在我的练习应用程序中的getline函数中输入分隔字符时,无需按Enter键即可停止输入。

目前,只有当用户在键入分隔符后按下Enter键,并且cout消息向用户解释时,来自getline的输入流才会中断,但我更希望在按下分隔符时简单地停止输入。

寻找如何在检测到指定字符时停止输入的建议。

这是我的完整代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define DEBUG 1 // 1 = enabled
int main()
{
char takeFirstNonSpaceCharacter(string text);
string message;
char stopper;
string stopperInput;
cout << "Type the character with which you want to signal end of a messagen";
cin >> stopperInput;
cin.ignore(128, 'n'); //clean cin
stopper = takeFirstNonSpaceCharacter(stopperInput); //in case input has white spaces
cout << "Type your message, make it as long as you want.n To finish typing enter the " << stopper << " symbol followed by Entern Input after the " << stopper << " symbol will be lostn";
getline(cin, message, stopper);
#if DEBUG == 1
cout << message << 'n';
#endif
system("pause");
return 0;
}
char takeFirstNonSpaceCharacter(string text)
{
string::const_iterator iter = text.begin();
while (iter != text.end())
{
if (*iter != 32 || *iter != 10 || *iter != 9) //ascii: 32 = space, 10 = new line, 9 = horizontal tab
{
return *iter; //if its not space character then it must be a character (unless the user can somehow type for example  on keyboard)
}
else
{
iter++; //if its space
}
}
return '';
}

输入/输出围绕这个(粗体是我的输入(

键入要用来表示消息结束的字符

}

键入你的信息,让它长到你想要的。完成键入输入}符号,然后输入输入丢失

asdf}asdf

asdf

使用standard-c/c++,在用户用回车键发送控制台输入之前,您将无法访问控制台输入。唯一的方法是直接访问终端,但由于每个操作系统使用不同的控制台,因此需要特定于操作系统的解决方案。

窗口上,您可以使用<conio.h>_getch()或WinApiReadConsoleInput执行此操作。

unix上,您可以使用<termios.h><curses.h>

跨平台表单库,适用于每个操作系统:
NCurses

同步ciolib

PDcurses

以下是windows的代码示例:

#include <conio.h>      // _getch()
#include <cctype>       // std::isspace
#include <string>       // std::getline
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if_not
#define DEBUG
int main(void)
{
int stopper;
std::cout << "Type the character with which you want to signal end of a message" << std::endl;
while (std::isspace(stopper = std::cin.get())) {} // oneliner instead of takeFirstNonSpaceCharacter
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // flush the rest of 
// alternative oneliner without the need of pressing enter
// do { stopper = _getch(); } while (std::isspace(stopper));
std::cout << "Type your message, make it as long as you want." << std::endl;
std::cout << "To finish typing, enter the " << (char)stopper << " symbol followed by Enter" << std::endl;
std::cout << "Input after the " << (char)stopper << " symbol will be lost" << std::endl;
std::string message;
for(int ch = _getch(); ch != stopper; ch = _getch()) {
_putch(ch); // print it, so the user can see his input
message.push_back(ch); // concat it to the message buffer
};
#ifdef DEBUG
std::cout << std::endl << message << std::endl;
#endif
getchar(); // system("pause"); is windows only, don't use that!
return 0;
}

注:

  • 如果您是c++新手,请尽量避免使用using namespace std;并习惯使用std::前缀
    为什么"使用命名空间std"被认为是不好的做法
  • 不要使用system("pause");
    系统("暂停"(;-为什么它错了
  • 如果宏仅用于布尔值,则不要将值用于宏。你可以使用#ifdef DEBUG,如果你不想使用它,就不要定义DEBUG
  • std::isspace可以替换takeFirstNonSpaceCharacter函数
  • 将长行拆分为多行以提高可读性