1

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:

Keshava GN
  • 4,195
  • 2
  • 36
  • 47
Shahbaj Sayyad
  • 141
  • 1
  • 1
  • 9
  • what is the incoming data why cant you just use cin >> b[i].title – Alper Fırat Kaya Dec 21 '15 at 11:21
  • Your getline caputed the first line break ( when you enter the number of books ) – Transcendental Dec 21 '15 at 11:22
  • What should happen if someone enters "3a" for the number of books? Should it produce an error? Should the `a` be ignored? Should the `a` be taken as the author? (You need to decide exactly what you want your code to do before you write it and make sure you write it to do what you want it to do!) – David Schwartz Dec 21 '15 at 11:27

3 Answers3

1

It is because of newline character that is sent to your first getline. Adding cin.get() after cin>>no_of_books; should fix it.

Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33
1

cin>>no_of_books doesn't consume \n.

so your next getline takes that now empty line.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

The problem is that cin >> no_of_books terminates when it sees that the next character in the input stream is a newline, but does not extract it. Then std::getline is asked to extract until a newline, but that's the very first character in the stream, so it just stops straight away.

What you can do with this depends on how you want your program to behave if the user enters something like this:

1 Old Man and the sea
Ernest Hemingway

In other words, the user follows the number of books directly with the title, without a newline in between.

To me, it seems that your application is line-oriented and should therefore ignore everything on the line after the number. If that's the case, you can ignoring everything up to (and including) the trailing newline like this:

cin >> no_of_books;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    Wouldn't that ignore the title if the title was entered on the same line as the number of books? Do you have some reason to think that's the desired behavior? – David Schwartz Dec 21 '15 at 11:28
  • @DavidSchwartz Hm, good point. The only reason I assume this to be desired behaviour is that the application seems to be directed at either interactive or line-based input. – Angew is no longer proud of SO Dec 21 '15 at 11:30
  • @DavidSchwartz Actually i'm trying to learn c++ using dev c++. So the input and output shows up on a command prompt type terminal (sorry i did not mention that i'm a beginner :p) – Shahbaj Sayyad Dec 21 '15 at 11:42