How to make getline() work when you just called get()

I just learned how to get only a character for input! (I did it back in 2011, but I think I have a better way. I just called cin.get(char). The only problem is, I can't use getline(cin, str) now (It skips right over my statement)! I am trying to call getline after using get. Is there anyway to tell the iostream that it is OK to start allowing lines (after you told it to get rid of ALL BUT ONE CHARACTER)?
You probably need to use cin.clear() to reset cin, depending on what was the outcome of the previous statements.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

this will allow the user to enter as many characters as he/she wants until he/she presses enter, without skipping over anything else in your program.

also this may be what your looking for...

#include <iostream>
#include <string>
using namespace std;
string a
cin >> a
cout << a << endl;

this will print out whatever phrase the the user types in

Last edited on
Thanks brandonator! Because it is a simple yes or no question I was asking the user, I just set the first parameter in cin.ignore() to an arbitrary number...
Topic archived. No new replies allowed.