Qt Creator inner terminal autoinputs 0 when there is cin stream
-
wrote on 5 Jan 2024, 08:28 last edited by JacobNovitsky 1 May 2024, 08:33
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
-
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? -
-
@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?wrote on 5 Jan 2024, 08:55 last edited by@jsulm
It comes from https://en.cppreference.com/w/cpp/string/basic_string/getline. I thought that line prior to thegetline()
just flushes anything which may be sitting in the buffer from before, does not affect the followinggetline()
? -
@jsulm
It comes from https://en.cppreference.com/w/cpp/string/basic_string/getline. I thought that line prior to thegetline()
just flushes anything which may be sitting in the buffer from before, does not affect the followinggetline()
?@JonB For me it does affect getline - I enter something, press enter, nothing happens, second enter then works.
-
@JonB For me it does affect getline - I enter something, press enter, nothing happens, second enter then works.
wrote on 5 Jan 2024, 09:03 last edited by JonB 1 May 2024, 09:05@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.
1/5