We have an assignment in c++ in which we have store information about multiple books(Author Name, Title, Publisher etc).
While getting the title of the book or name of the author I tried to take the input in the form of a sentence using the function getline(), but while execution it does not asks me the title (which I intended it to) instead it directly asks me the author's name.
Here is the code:
#include<iostream>
using namespace std;
class BookInfo
{
public:
string title,author,publisher;
int price,stock_position;
};
class Books
{
BookInfo b[10];
int no_of_books;
public:
void getdata()
{
cout<<"Enter the number of books: ";
cin>>no_of_books;
for(int i = 0 ; i < no_of_books ; i++)
{
cout<<"Title: ";
getline(cin,b[i].title,'\n');
cout<<"Author: ";
getline(cin,b[i].author,'\n');
cout<<"Publisher: ";
getline(cin,b[i].publisher,'\n');
cout<<"Price: ";
cin>>b[i].price;
cout<<"Stock Position: ";
cin>>b[i].stock_position;
}
}
};
int main(void)
{
Books a;
a.getdata();
}
Here is the output:
Enter the number of books: 1
Title: Author: