我对Eclipse控制台有问题,似乎我的输入没有正确传递。这是一个新的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;
}
}
}
Eclipse控制台:
>> 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:\Users\Andy>eclipse-workspace\stacktest\Debug\stacktest.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
答案 0 :(得分:0)
有趣。在我的机器上它可以工作。
所以它不起作用的唯一原因是:“退出”不等于输入。输入的末尾可能有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 << "\n\nSize 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;
}
}
}
我很好奇,结果会怎样。 。