c++ – Why is it necessary to use the cin.ignore() command after using "cin >>" and then just using cin.getline(char*,size)?

Question:

(I apologize for the title, I'm hard to explain myself there, but in the description everything will be understood)

Today during a project I did, I was presented with an error that had never happened to me before, and the truth is that I would not resort to this community if it were not for the fact that not even my programming master knows why this error occurs.

The error occurs in this portion of code:

#include <iostream>
#include <stdlib.h>

int main()
{
    char frase[100];
    int opcion;
    std::cout << "Ingrese una opcion: ";
    std::cin >> opcion;
    switch (opcion)
    {
    case 1:
        std::cout << "Ingrese la frase:";//El programa ignora este comando
        std::cin.getline(frase, 100); //<--por algún motivo
        std::cout << "La frase es: " << frase << std::endl;
        break;
    default:
        std::cout << "Opcion no valida..." << std::endl;
        break;
    }
    system("pause");
}

EXIT:

Enter an option: 1
Enter the phrase:The phrase is:
Press a key to continue . . .

As you can see, in the output, it correctly reads the int opcion , but for some reason completely ignores the getline ;
Searching the internet, I found the solution:

#include <iostream>
#include <stdlib.h>
//#include <windows.h>
//#include <string.h>
//#include <fstream>
//#include "ClaseA.h"

int main()
{
    char frase[100];
    int opcion;
    std::cout << "Ingrese una opcion: ";
    std::cin >> opcion;
    std::cin.ignore(); //<-- la solución, poner esto justo después de usar
    switch (opcion)    //    el 'cin >>'
    {
    case 1:
        std::cout << "Ingrese la frase:";
        std::cin.getline(frase, 100);
        std::cout << "La frase es: " << frase << std::endl;
        break;
    default:
        std::cout << "Opcion no valida..." << std::endl;
        break;
    }
    system("pause");
}

EXIT:

Enter an option: 1
Enter the phrase: Programmers don't sleep
The phrase is: Programmers do not sleep
Press a key to continue . . .

however, there are things I don't understand, for example:
why does this error occur?
Why is it mandatory to use that command after a cin >> ?

I just want to know why this happens.

Answer:

why does this error occur?

It is not an error but the expected behavior. I explain.

cin has certain aids that simplify the reading of data. So, the following code:

int a, b;
std::cin >> a >> b;
std::cout << a << ' ' << b;

will work correctly whether there is a space between the two numbers, twenty spaces, a dozen newlines, or a combination. In these cases cin is able to ignore a series of characters identified as separators.

In this case, cin discards any separators that come before the sequence you intend to read. Any separators after it, and this is important , including line breaks, are kept in the input buffer.

However, cin.getline does not have these helpers and the reason is that you are not asking cin to read you a simple string of characters, that is, something like:

std::string cadena;
std::cin >> cadena;

In this case, a cadena would store a sequence of characters until it reaches a separator (generally space, tab or line break).

You're telling cin that it has to read every character it finds up to the first newline found… and it turns out that if you don't clear the buffer, cin detects that the first character read is a newline, so stop reading.

The getline method cannot discard that line break because then the function would have problems reading lines that can be empty (in this case the line break would be discarded and the line following the one requested would be read).

So, if before using getline you used the extraction operator >> , remember to use ignore to discard the line break that has been left in the input buffer.

Scroll to Top