Eclipse控制台无法正确解析输入
Eclipse console not parsing input properly
我对日食控制台有一个问题,看来我的输入未正确传递。这是一个新的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;
}
}
}
我真的很好奇,结果是什么。。。
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么控制台要求输入,即使代码中没有输入
- 防止控制台在通道字符(在 c++ 中)中输入空格(即空格、制表符和换行符)
- 跳转到通过输入坐标定义的控制台屏幕位置
- 控制台未正确读取输入字符串
- 直接输入控制台应用程序
- 当被一个输入分开时,有两个控制台输出同时显示
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- 如何从 c++ 控制台读取未知行数(允许空行)上的输入
- 如何将控制台的输入存储到 wchar_t 变量?
- 从 R 控制台获取用户输入:Rcpp 和 std::cin
- 如何将用户输入从控制台传递到不同线程中的对象 -> QTcpsocket
- 将文本发送到输入行控制台 C++
- 如何在 eclipse 中将标准输入从控制台更改为另一个程序
- 如何防止父进程在孩子死后丢失控制台输入?
- 如何使用字符串获取用户控制台输入
- C 程序在获取输入[控制台应用程序]时跳过行
- 不暂停代码的用户输入(c++控制台应用程序)
- 实时键盘输入控制台(在Windows中)