How can I clean the buffer so that it does not cache the '\n'
You're already doing it:
cout<<"member_code: "; //NUMERICO
cin>>member_code;
cin.ignore(); // <<--- AQUI!!!
That call removes the first character from the input buffer.
The different overloads of the extraction operator >>
do not eliminate line breaks or spaces after reading ... what they do is ignore these characters before starting the reading. So, the following example will read two integers regardless of whether you separate them with spaces (one, two or whatever) or line breaks:
int a, b;
std::cin >> a >> b;
std::cout << a << '-' << b;
In your case the program is working correctly ... the only weird is the reading of the field that follows the integer:
cout<<"member kob: "; //CADENA
cin.getline(member_kob,2);
In this reading you are telling cin
that you can not store more than two characters in member_kob
... taking into account that one of them will be yes or yes the chain terminator ( member_kob
) , we have that in std::string
will store only one character.
If you do not need to read spaces, it is preferable to use >>
and the extraction operator >>
. The solution will be cleaner and give you less headaches:
std::string informativo;
cout<<"informativo: "; //1.-CADENA
cin >> informativo;
std::string nivel_desactualizada;
cout<<"nivel_desactualizada: "; //2.-CADENA
cin >> nivel_desactualizada;
cout<<"abierta_cerrada: "; //3.-CADENA
cin.getline(abierta_cerrada,10);
int member_code;
cout<<"member_code: "; //NUMERICO
cin>>member_code;
std::string member_kob;
cout<<"member kob: "; //CADENA
cin >> member_kob;
std::string no_cuenta;
cout<<"no_cuenta: "; //CADENA
cin >> no_cuenta;
Or, if in some field it turns out that it is possible to find spaces:
std::string informativo;
cout<<"informativo: "; //1.-CADENA
getline(cin,informativo);
In the latter case, you will have to worry about retiring the residual line break after using the operator ignore
:
int member_code;
cout<<"member_code: "; //NUMERICO
cin>>member_code;
cin.ignore();
std::string member_kob;
cout<<"member kob: "; //CADENA
getline(cin,member_kob);
However, there is a more generic way to use the %code% method, and it is asking you to discard all the characters you find until the first line break (which will also be discarded):
#include <limits>
cin.ignore(std::numeric_limits<int>::max(),'\n');