I have a problem with the Eclipse console where it appears that my input is not being passed in properly. This is with a new Hello World C++ project. Eclipse console loops endlessly, but running from Windows command line or Cygwin terminal works fine. I've played about with the console encoding to no avail.
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 console:
>> 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 command line:
C:\Users\Andy>eclipse-workspace\stacktest\Debug\stacktest.exe
>> exit
This is loop number 1
Exiting
EDIT
With thanks to @Armin it seems that Eclipse is inserting a new line at the end of the input.
>> 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
Interesting. On my machine it works.
So the only reason, why it would not work is: "exit" is not equal to input. There may be a CR or LF or CR/LF or other character at the end of the input. Or, we have different char Types.
Please run the following test program:
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;
}
}
}
I am really curious, what the result will be . . .