Eclipse 控制台无法正确解析输入
Eclipse console not parsing input properly
我的 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
有意思。在我的机器上它工作。
所以它不起作用的唯一原因是:"exit" 不等于输入。输入的末尾可能有 CR 或 LF 或 CR/LF 或其他字符。或者,我们有不同的 char 类型。
请运行以下测试程序:
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;
}
}
}
我很好奇,结果会怎样。 . .
我的 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
有意思。在我的机器上它工作。
所以它不起作用的唯一原因是:"exit" 不等于输入。输入的末尾可能有 CR 或 LF 或 CR/LF 或其他字符。或者,我们有不同的 char 类型。
请运行以下测试程序:
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;
}
}
}
我很好奇,结果会怎样。 . .