简体   繁体   English

带有 getline 的循环不会因用户输入而结束

[英]While loop with getline doesn't end for user input

I thought getline stops at a newline character, but the while loop does not end?我认为getline在换行符处停止,但是 while 循环没有结束? it returns the correct data but it just sits in the terminal window.它返回正确的数据,但它只是位于终端 window 中。 For example:例如:

 Enter an expression: #5+4#5+4 (blinking cursor)

(can enter data forever and press enter forever and it wont exit) (可以永远输入数据,永远按回车不会退出)

my code, (main.cpp):我的代码,(main.cpp):

    int main()
    {
    string exp;
        cout << "Enter an Infix Expression:";

        while (getline(cin, exp, '#'))
        {
            string token = exp;
            string post;
            cout << token << endl;
            IntoPost *infix = new IntoPost(token.length());
            post = infix->inToPost(token);
            cout << post << endl;
        }
        cin.get();
    }

The Solution Using EOF使用 EOF 的解决方案

Your current program is looping endlessly because getline returns std::basic_istream , so while(getline()) will never equate to 'false'. 您当前的程序无限循环,因为 getline返回 std::basic_istream ,因此 while(getline()) 永远不会等同于“假”。

As @0x499602D2 has stated, your program is working as intended, but the extraction from getline can only end in two ways, as indicated by the reference here :正如@0x499602D2 所说,您的程序正在按预期工作,但从 getline 的提取只能以两种方式结束,如参考此处所示:

Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for when no delimiter is specified).从 is 中提取字符并将它们存储到 str 中,直到找到分隔符 delim(或换行符,'\n',如果没有指定分隔符)。

The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.如果在 is 中到达文件末尾或在输入操作期间发生某些其他错误,提取也会停止。

The first condition is difficult to pull off, as inputs on console are triggered by the \n character.第一个条件很难实现,因为控制台上的输入是由 \n 字符触发的。

As for the second condition, as per @DavidC.Rankin:至于第二个条件,根据@DavidC.Rankin:

You can also generate a manual EOF on Linux with [Ctrl+d] or windows with [Ctrl+z] (generally twice is required)您还可以使用 [Ctrl+d] 在 Linux 上生成手动 EOF,或者使用 [Ctrl+z] 在 windows 上生成手动 EOF(通常需要两次)

This means the solution is to use [Ctrl+d] or [Ctrl+z] to trigger the second condition to end your while loop at any time.这意味着解决方案是使用 [Ctrl+d] 或 [Ctrl+z]触发第二个条件以随时结束您的 while 循环。


Alternative Using a Break Statement使用 Break 语句的替代方法

One alternative way you can try to end the loop instead is breaking on input of an 'exit' string:您可以尝试结束循环的另一种方法是中断“退出”字符串的输入:

(1) (1)

#include <algorithm>
//...
    while (getline(cin, exp, '#'))
    {
        // removes meaningless endline chars from input
        exp.erase(std::remove(exp.begin(), exp.end(), '\n'), exp.end()); 
        if (exp == "exit"){
            break;
        }
        //... Your While Block Code Here!
    }

To break out of your while loop, you can simply use:要跳出你的while循环,你可以简单地使用:

exit#出口#

# Note, the endls from your couts in the loop will bleed into your inputs on your next while (getline(cin, exp, '#')) , giving us unwanted newlines. # 注意,循环中endls中的couts将在下一个while (getline(cin, exp, '#'))渗入你的输入,给我们不需要的换行符。 To prevent this, we can get rid of the endlines from the inputs by using std::erase().为了防止这种情况,我们可以使用 std::erase() 去除输入中的结束线。 If you wish to keep those endlines in your input, simply set string token = exp;如果您希望在输入中保留这些结束行,只需设置string token = exp; in front of the erase() line.在 erase() 行前面。

That's right, getline blocks the execution of the loop until a line separator is received and returns while that all is well, in the next step everything is repeated.没错,getline 会阻塞循环的执行,直到收到行分隔符并返回,而一切都很好,在下一步中,一切都将重复。 If you want the loop not to be infinite - then put the Boolean variable key in the loop condition, and from the input check if the last character is an exit symbol and if so switch the variable key如果您希望循环不是无限的 - 然后将 Boolean 变量键放在循环条件中,并从输入中检查最后一个字符是否是退出符号,如果是则切换变量键

Well, in a loop you must include some sort of operator.好吧,在循环中,您必须包含某种运算符。 Right now your code reads:现在您的代码如下:

while (getline(cin, exp, '#'))
{
   //code
}` 

And the problem with this is that it's basically saying,问题在于它基本上是在说,

while(exp == true){
//Execute code forever
}

In order for your loop to stop, you must pass some sort of argument.为了让你的循环停止,你必须传递某种参数。 Example:例子:

while(getline(cin, exp, '#') == "What ever you want"){
//Exicute this code while exp is what I want
}

I hope you can implement this into your project, although I don't know specifically what you want your loop to do, so just remember to pass an argument that allows your while loop to stop at some point.我希望你可以在你的项目中实现它,虽然我不知道你想要你的循环做什么,所以只要记住传递一个参数,让你的 while 循环在某个时候停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM