Why is getline so inconsistent?

I am in a computer lab, and none of the teachers can understand why mine getlineis not working properly. It does not save information correctly (stores only 1 or 2 letters). Does anyone know why this is so?

void addMovie(Inventory movie[], int &count)
{
    string s;
    int i;

    cout << "Please enter the SKU " << endl;
    cin >> i;
    movie[count].sku = i;

    cout << "Please enter the name of the movie you wish to add " << endl;

    cin.ignore('\n');
    getline(cin, s, '\n');
    movie[count].title = s;

    count++;
}
+5
source share
1 answer

std::istream::ignore(i.e. cin.ignore()) the first argument is the number of characters to be discarded. The value '\n'has an ASCII code of 10, so it is '\n'implicitly converted to an integer (most likely 10, but may differ when using different encoding - EBCDIC uses 21) and that the characters are ignored, leaving a few remaining.

, :

#include <limits> //for numeric_limtis
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
+8

All Articles