Qt Creator inner terminal autoinputs 0 when there is cin stream
-
How to solve it?
#include <iostream> #include <limits> // for std::numeric_limits int main() { std::string key; std::cout << "Enter a key: "; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear input buffer std::getline(std::cin, key); // Output the first character's ASCII value if (!key.empty()) { std::cout << "Key: " << int(key[0]) << std::endl; } else { std::cout << "Key is empty." << std::endl; } return 0; }
if change to gnome-terminal then back it does the trick
-
@JacobNovitsky said in Qt Creator inner terminal autoinputs 0 when there is cin stream:
std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n');
This line is the problem - first enter press is ignored.
Why don't you use std::cin.clear(); instead? -
-
-
@jsulm
I think then that careful reading of that item must mean you are only supposed to use it if you know there is something already sitting there left over from before and you want to flush it. It must mean ignore everything up to the next newline in the buffer, whenever that is. The OP should certainly try removing it. I don't know what the OP's "autoinputs 0" means, nor why it apparently has different effect in same situation from gnome-terminal.