C/C++连续两次输入 Enter 后或在 2 个换行符后如何从 stdin 读取
我试图让用户向控制台输入一段文本,只有在连续按两次 Enter 后,或者换句话说,当在已经是空白的行上按 Enter 时,我的程序才会从 stdin 读取该文本块。但是,我想继续从标准输入读取,所以基本上,只有在已经是空行的情况下按下 Enter 时才从标准输入读取。然后冲洗并重新启动。
用户输入示例:
Hello(\n)
World(\n)
(\n)
(Read Now)
我一直无法找到能够指定此行为的输入函数。
另外,我尝试了一些使用单字符检索函数的方法,但我无法让它正常工作。
有人知道如何优雅地做到这一点吗?
答案已实现:
char word[100];
int i = 0;
while ((word[i++] = getchar())!='\n' || (word[i++]=getchar())!='\n');
printf("%s",word);
显然,在使用它之前需要处理缓冲区溢出。只是一个例子。
I'm trying to have the user input a block of text to the console that my program will read from stdin only after enter has been hit twice in a row or, said in a different way, when enter is hit on an already blank line. But, I want to continue reading from stdin so basically, only read from stdin when enter is hit while on an already blank line. Then flush and restart.
Example of user input:
Hello(\n)
World(\n)
(\n)
(Read Now)
I haven't been able to find an input function capable of specifying this behavior.
Also, I've tried a few methods using single character retrieval functions but I haven't been able to get it work correctly.
Anyone know of a way to do this elegantly?
Answer implemented:
char word[100];
int i = 0;
while ((word[i++] = getchar())!='\n' || (word[i++]=getchar())!='\n');
printf("%s",word);
Obviously buffer overflow needs to be dealt with before using this. Just an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,您希望忽略序列
\n\n
之前的输入。你可以这样做:Basically, you want to ignore the input until the sequence
\n\n
. You can do it with:忽略所有内容,直到读到一个空行,然后停止忽略。
Ignore everything until you read in a blank line, then stop ignoring.
您可以创建一个过滤流缓冲区,该缓冲区会在行进入时读取行,但会阻止转发字符,直到看到两个换行符为止。然后它可以假装这就是所期望的一切,直到有东西重置它。代码看起来像这样:
您将使用这样的流缓冲区(如果您想通过 std::cin 来使用它,您可以使用 std::cin.rdbuf() 来替换
std::cin
的流缓冲区):显然,如何玩这个游戏有很多变化......
You can create a filtering stream buffer which reads lines as they come in but blocks forwarding characters until it has seen two newlines. It could then pretend this is all what is to be expected until something resets it. The code would look something like this:
You would use this stream buffer something like this (if you want to use it via
std::cin
you can usestd::cin.rdbuf()
to replacestd::cin
's stream buffer):Obviously, there are many variations on how to play this game...