c++ - Input buffer not clearing -
i encoutering problem cin object of c++. use microsoft visual studio 2013 ultimate edition ide.
#include<iostream> #include<stdio.h> using namespace std; int main() { char inp[20], inp2; cout<<"input\t"; cin>>inp; cout<<"input char"; inp2=getchar(); cout<<inp<<endl<<inp2; }
the problem after reading input first cin line getchar reading new liine charector. i.e cin leaves new line charecter in buffer after done taking input. getchar reads nw line charector doesnt take input user @ all.
after first input processed, can use cin.ignore()
ignore rest of line.
cin>>inp; // ignore rest of line. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cout<<"input char"; inp2=getchar();
Comments
Post a Comment