0

So I have this problem with getline() in c++.After running the code, the output got jump down to "insert author", I've used cin.ignore() and got the first letter of the name dissapeared. How do I tackle this problem Output: Insert the name Insert author

This is my code

#include <iostream>
using namespace std;
    class Book
    {
        public:
            string name;
            string author;
        void input()
        {
            cout << "insert the name\n";
           getline(cin,name);
           cout << "insert author\n";
           getline(cin,author);
        }
        void output()
        {
            cout << name << endl;
            cout << author << endl;
        }
    };  

int main()
{
    int n;
    cout << "Insert how many books do you want to create\n"; 
    cin >> n;
    Book b[100];
    for(int i=0;i<n;i++)
    {
        b[i].input();
    }
    for(int i=0;i<n;i++)
    {
        b[i].output();
    }
}
Chris
  • 26,361
  • 5
  • 21
  • 42

0 Answers0