Multiple inputs on one line

Yes, you can input multiple items from cin, using exactly the syntax you describe. The result is essentially identical to: cin >> a; cin >> b; cin >> c; This is due to a technique called “operator chaining”. Each call to operator>>(istream&, T) (where T is some arbitrary type) returns a reference to its first … Read more

cin.ignore(numeric_limits::max(), ‘\n’)

This line ignores the rest of the current line, up to ‘\n’ or EOF – whichever comes first: ‘\n’ sets the delimiter, i.e. the character after which cin stops ignoring numeric_limits<streamsize>::max() sets the maximum number of characters to ignore. Since this is the upper limit on the size of a stream, you are effectively telling … Read more

cin for an int inputing a char causes Loop that is supposed to check input to go wild

When an error occurs when reading from a stream, an error flag gets set and no more reading is possible until you clear the error flags. That’s why you get an infinite loop. cin.clear(); // clears the error flags // this line discards all the input waiting in the stream cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’); Also, it’s wrong … Read more

cin input (input is an int) when I input a letter, instead of printing back incorrect once, it prints correct once then inc for the rest of the loop

There are a number of ways to structure the various test, but when taking input with cin (or generally with any input function), you must account for any characters in the input buffer that remain unread. With cin, you have three conditions you must validate: .eof() (eofbit) is set. Either the end of input was … Read more

How to reset std::cin when using it?

You could use, when the condition, std::cin.fail() happens: std::cin.clear(); std::cin.ignore(); And then continue with the loop, with a continue; statement. std::cin.clear() clears the error flags, and sets new ones, and std::cin.ignore() effectively ignores them (by extracting and discarding them). Sources: cin.ignore() cin.clear()

Why is this cin reading jammed?

cin.clear() doesn’t clear the standard input. What it does is clearing error bits, like eofbit, failbit and others, and sets the stream into a good state. Maybe you expected it to clear out anything in it? If the user typed yes no Just before, and you cin >> someStringVariable; It will read up to no … Read more

How to cin values into a vector

As is, you’re only reading in a single integer and pushing it into your vector. Since you probably want to store several integers, you need a loop. E.g., replace cin >> input; V.push_back(input); with while (cin >> input) V.push_back(input); What this does is continually pull in ints from cin for as long as there is … Read more

Correct way to use cin.fail()

std::cin.fail() is used to test whether the preceding input succeeded. It is, however, more idiomatic to just use the stream as if it were a boolean: if ( std::cin ) { // last input succeeded, i.e. !std::cin.fail() } if ( !std::cin ) { // last input failed, i.e. std::cin.fail() } In contexts where the syntax … Read more

Hide user input on password prompt [duplicate]

From How to Hide Text: Windows #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; GetConsoleMode(hStdin, &mode); SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); string s; getline(cin, s); cout << s << endl; return 0; }//main cleanup: SetConsoleMode(hStdin, mode); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); Linux #include <iostream> #include <string> … Read more