Eclipse控制台无法正确解析输入

Eclipse console not parsing input properly

本文关键字:输入 控制台 Eclipse      更新时间:2023-10-16

我对日食控制台有一个问题,看来我的输入未正确传递。这是一个新的Hello World C 项目。Eclipse控制台循环无休止地循环,但是从Windows命令行或Cygwin终端运行正常。我已经玩过编码的控制台。

int main() {
    int times;
    while (true) {
        cout << ">> " << flush;
        // Get input from the command line
        string input;
        getline(cin, input);
        cout << "This is loop number " << times << endl;
        times++;
        if (input == "exit") {
            cout << "Exiting" << endl;
            return 0;
        }
    }
}

日食控制台:

>> exit
This is loop number 1
>> exit
This is loop number 2
>> exit
This is loop number 3
>> exit
This is loop number 4
>> exit
This is loop number 5
>> exit
This is loop number 6
>> exit
This is loop number 7
>> 

Windows命令行:

C:UsersAndy>eclipse-workspacestacktestDebugstacktest.exe
>> exit
This is loop number 1
Exiting

编辑

感谢@armin,Eclipse似乎在输入末尾插入了一条新线。

>> hello
This is loop number 0

Size of input6   Input: 'hello
'
Char: h   int representaion: 104
Char: e   int representaion: 101
Char: l   int representaion: 108
Char: l   int representaion: 108
Char: o   int representaion: 111
Char: 
   int representaion: 13

有趣。在我的机器上它有效。

因此,唯一不起作用的原因是:"退出"不等于输入。输入末尾可能有CR,LF或CR/LF或其他字符。或者,我们有不同的炭类型。

请运行以下测试程序:

include <iostream>
#include <iomanip>
#include <string>
int main() 
{
    int times{ 0 };
    while (true) {
        std::cout << ">> " << std::flush;
        // Get input from the command line
        std::string input{};
        std::getline(std::cin, input);
        std::cout << "This is loop number " << times << std::endl;
        times++;
        // Test Begin ----------------------------------------
        std::cout << "nnSize of input" << input.size() << "   Input: '" << input << "'n";
        for (char c : input) {
            std::cout << "Char: " << c << "   int representaion: " << static_cast<unsigned int>(c)<< 'n';
        }
        // Test End----------------------------------------
        if (input == "exit") {
            std::cout << "Exiting" << std::endl;
            return 0;
        }
    }
}

我真的很好奇,结果是什么。。。