Aseel:
|
cin.ignore(numeric_limits<streamsize>::max(), '\n');
|
In fact this piece of code is it, why helios say >> and getline are not getting along.
The problem is that you readin a single character:
After this, if say, i typed '1' + Enter you read the character '1', but >> leaves the Enter in the input queue. If you now want to read a line with say getline, than getline will read the empty line before you can type anything. Because the Enter left by >> is still there.
Reminder: You type
1. Add word
Your option is: 1\n
|
1 will be read as option but \n is still there.
So the next getline is automaticly back, because he will read: "\n" as line.
Next stop, what if:
1. Add word
Your option os: 1i write a lot here blahblah.\n
|
Now whats happening? You will read 1 as character option and you leave the rest behind.
To stop this from happening, the ignore function is there.
|
cin.ignore(amount_of_chars_ignored, ignore_until_i_read_this)
|
it is used like this:
1 2 3 4 5 6 7 8 9 10
|
// ignore the next 4 chars or until you read a ' '
cin.ignore(4, ' ');
// ignore the next 10 chars or until you get an EOF
cin.ignore(10, EOF);
// ignore the next 10 chars or until you get an EOF (equivalent to upper)
cin.ignore(10);
// And now the gag. To get numerical limits in computation C++ has some facilities.
// IGNORE AS MUCH AS POSSIBLE until new line ;-)!
cin.ignore(numeric_limits<streamsize>::max(), '\n');
|
Maikel