0

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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Please show a [mcve]. A `main` function doing a single `addBookData();` should not skip the first one, but you do have a problem in the middle regardless: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – chris Jun 01 '21 at 04:58
  • @chris, no, it doesn't. The first input gets skipped, it is not taking any input from the user and jumping to the second getline(). The link you've referred to explains string not getting printed after user submits an input. In my case, it doesn't even ask the user for any input and jumps to the second getline(). –  Jun 01 '21 at 05:05
  • The purpose of that link was to explain a problem that definitely exists within the middle of your function between the `cin >>` and `cin.getline` call, and that's consistent with the comments you put there. I suspect the first one is getting skipped due to the same problem except that the `cin >>` call is outside of this function before the function is called. And yes, that question still applies. If you run it interactively, you won't have a chance to enter the second line shown in the question. Please read the answer carefully and you can see how it applies directly to this question's code. – chris Jun 01 '21 at 05:13
  • 1
    @node_modules The duplicate questions and answers say nothing about output. They solely deal with the input. When someone asks why getline is getting skipped, IME the problem is getline after >> 100% of the time. If your question is an exception, please post a [mcve] and I will gladly un-duplicate it. Note that in your code the problem definitely exists, you are doing getline after >> in the middle of your function. Maybe the first time around the problem is different, but we have no way of verifying it without seeing what happens before the first getline – n. m. could be an AI Jun 01 '21 at 05:16

0 Answers0