That is because you have a trailing new line
(from Enter) character remaining on the stream that is not read by the first read operation. So the first gets(book_name)
will read that and continue to the next request for input.
use getline
to remove any remaining offending input from the stream.
void Book::add(){
string garbage;
getline(cin,garbage); // this will read any remaining input in stream. That is from when you entered 'a' and pressed enter.
cout << "Enter name of Book: ";
gets(book_name);
getline(cin,garbage); // this will read any remaining input in stream.
cout << "\n\nPlease enter the book id: ";
cin >> book_id;
Anyways just use the safer way of reading input from streams
cin >> book_name;
instead of gets
. Then you will not have such problems.
if you want to read space separated inputs in to one string the use std::getline (like I did for garbage above)
std::getline(cin,book_name);