I'm having a hard time with reading data from keyboard and storing it in a struct variable, on a C++ console program.
My struct contains data about a book (name, genre and isbn) and looks like this:
1 2 3 4 5 6
|
struct libro
{
string nombre;
string genero;
long isbn;
};
|
Now I'm trying to fill in that info from keyboard, in this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void agregar_libro()
{
libro un_libro;
cout << "\n\n\nAGREGAR LIBRO \n" << endl;
cout << "Nombre: ";
getline(cin, un_libro.nombre);
cin.ignore();
cout << "Genero: ";
getline(cin, un_libro.genero);
cin.ignore();
cout << "ISBN: ";
cin >> un_libro.isbn;
cin.ignore();
}
|
When running in debug mode I can see the info being correctly stored in the corresponding struct members. However, I also get an extra new line after each entry. So, whenever I enter data, I have to press Return twice: once for the info to be entered and once again to dismiss a new line that is generated and get the next prompt. So I get something like this:
Nombre: example name
Genero: example genre
ISBN: 12345
And if I use cin.ignore(INT_MAX, '\n'); I get the exact same result.
I've also tried using just cin.ignore(INT_MAX); but then I get the first prompt ("Nombre; "), I'm able to enter the data and after I press Return I never get the next prompt (I can keep pressing Return and generating new lines but nothing else happens).
So how exactly should I load those strings into my struct? I then need to write the struct members into a text file, but that's a different topic :)
Thanks!