A problem with cin after a getline makes my while loop never end at the second iteration in c++

Viewed 21

If I replace the getline for a cin, just as a test, it works as I want. But the moment I introduce the getline it creates a never-ending loop at the second iteration. I think it has something to be with the buffer but I don't know how it works so I need help.

This is the code:

while(true)
 {
      alumno++;
     cout<<"Alumno "<<alumno<<":"<<endl;
     getline(cin,nombre_alumno);
     if(nombre_alumno == "EXIT")break;

     cin>>nota;

 }
1 Answers

After this statement

cin>>nota;

insert

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

You will need to include the header

#include <limits>
Related