C ++ Iterator while jumping the reading of cin.getline

0

I'm starting in C ++, during some tests I noticed that after the second iteration of while the command cin.getline was not read displaying the line after it. I do not understand what happens. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main(void) {
    char matter[20] = " ";
    string category(" ");
    ofstream write;
    bool control = true;
    char op = ' ';

    while (control) {
        cout << "Enter matter: ";
        cin.getline(matter, 20);//--------
        write.open("database.txt" , ofstream::app);
        write << "->" << matter << "\n";
        write.close();
        cout << "Prosseguir?[Y/N]:  ";
        cin >> op;
        op = toupper(op);
        if (op == 'N') {
          return 0;
        }
    }
}

If anyone knows what is ...

    
asked by anonymous 24.06.2017 / 01:33

1 answer

0

The solution is very simple, just put cin.ignore(); before if.

see funcioanndo

Read the article: C ++: Using cin.ignore ()

I've even had this same problem in the past See a little discussion of the topic to understand even more!

    
24.06.2017 / 06:02