-3

I try to use getline after cin but the compiler ignore it and go directly to next input

int id , age ;
String name;

cin >> id;
getline (cin , name ) ;
cin >> age ;
  • 1
    There should be quite a few duplicates about this, but the short of it is your `Enter` key that you pressed after inputting `id`. Think about what happens with that key. – Some programmer dude Mar 28 '18 at 15:49
  • 3
    We see a lot of questions from Cairo University. Have you been instructed to use Stack Overflow in this way? Please take the [tour] and have a look at the [help/on-topic]. Also see: [ask]. Please share the links with your classmates. – wally Mar 28 '18 at 15:49

2 Answers2

0

The problem in your program is when you input the id and hit enter at runtime that space is considered as an input and stays in input buffer until you consume it. So, you need to consume that space. So it is easy to think that C++ compiler skips call to getline(cin,name) function but it is actually not.

0

You can use cin.ignore() before getline .. as getline ends when it see \n in the stream .. and in cin instruction the user must press enter to end the input .. so the getline found it in buffer and the compiler will skip it .. so you must ignore it from stream

cin >> id;
cin.ignore(100,'\n');
getline(cin,name);