Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getline() skipping first even after clear()

Tags:

People also ask

Why does C++ Getline skip?

When execution reaches getline(cin, rp. command) the program just print "Enter repo command: " and skips the getline(cin, rp. command) line so that the user is not given time to respond.

Does Getline clear buffer?

The getline() function should not leave anything behind, it extracts the end of line character and discards it, so you should not need the ignore() at all in this case.

Does Getline skip newline?

getline(cin, newString); begins immediately reading and collecting characters into newString and continues until a newline character is encountered. The newline character is read but not stored in newString.

Does Getline ignore leading whitespace?

The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters.


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
}