当用户键入分隔符时,停止getline()输入
Stop getline() input when user types delimiter character
我正试图找到一种方法,当用户在我的练习应用程序中的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函数
- 将长行拆分为多行以提高可读性
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 遇到新行时,有没有办法停止istream_iterator
- 线程之间的布尔停止信号
- 如何正确停止g_main_loop_run
- 当用户键入分隔符时,停止getline()输入
- MPI突然停止了对多个核心的操作
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- getline() 的原型/库是什么;
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 如何使用隔离>终止执行来停止所有线程
- 我可以使用什么来停止循环而不是"返回 0"?
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- Getline(); 提取以 1A 字符停止
- progam在std :: getline AM之后停止响应,代码中是否存在错误
- 如何自动停止 getline() 循环在 cin 上
- 注意:(getline不是问题所在)C++getline()停止在用户定义的函数中工作,但在主函数中工作
- 停止getline丢弃换行符
- C++ getline(cin,buffer)在我按 CTRL+Z 时不会停止
- c++ getline()不停止在行尾