I am developing a program where I am calling cin.getline()
to get values from the user. My program is logically correct and program runs but, it somehow skips the first cin.getline()
method and calls the second one and onwards.
Here's my code:
public:
Library()
{
myFile.open("testFile.txt", ios::app);
myFile2.open("testFile.txt", ios::in);
cout<<"Constructor calling"<<endl;
}
void addBookData()
{
cout<<"Enter a Book Name: "; // this gets printed
cin.getline(bookName, 50); // skips this, doesn't take input from user
cout<<"Enter the Author's Name: "; // this gets printed
cin.getline(authorName, 50); // takes value from the user here
cout<<"Enter the Genre: ";
cin.getline(genre, 30);
cout<<"Enter number of pages: ";
cin >> noOfPages;
cout<<"Enter rating: ";
cin >> rating;
cout<<"Enter language: "; // again this gets printed
cin.getline(language, 20); // this gets skipped as well
cout<<"Issued to (Username): ";
cin.getline(issuedTo, 30); // takes value from here
cout<<"Issued date: ";
cin.getline(issuedDate, 30);
myFile <<"Book Name: " << bookName << endl << "Author's Name: " << authorName << endl <<
"Genre: " << genre << endl << "Number of pages: " << noOfPages << endl << "Rating: " <<
rating << endl << "Language: " << language << endl << "Issued to (Username): " << issuedTo
<< endl << "Issued date: " << issuedDate << endl;
}
Can anybody explain to me why is this happening? What am I doing wrong? Any help would be appreciated. Thanks.