So I have a function that keeps skipping over the first getline and straight to the second one. I tried to clear the buffer but still no luck, what’s going on?
void getData(char* strA, char* strB)
{
cout << "Enter String 1: "; // Shows this line
cin.clear();
cin.getline(strA, 50); // 50 is the character limit, Skipping Input
cout << endl << "Enter String 2: "; // Showing This Line
cin.clear();
cin.getline(strB, 50); // Jumps Straight to this line
}
Make sure you didn’t use
cin >> str. before calling the function. If you usecin >> strand then want to usegetline(cin, str), you must callcin.ignore()before.In case of using c-strings:
ADD: Your wrong is not probably in the function itself, but rather before calling the function. The stream
cinhave to read only a new line character\n'in firstcin.getline.