简体   繁体   中英

Why is cin.ignore() ignoring the first character of my getline input?

So according to Eclipse, the getline function stores the entire text line from stream along with the newline and "terminating null character." I don't entirely know what that means.

The output I'm trying to get is printing two parallel arrays using a function. This function is a void function holding 3 parameters, the first array, the second array, and the constant integer array size value.

I'm able to store input into the first array with no problem.

    const int SIZE = 5;
    double firstArray[SIZE];

    // range-based `for` loop to get user input
    for (double& floatNum : firstArray)
    {
        cout << "Enter a floating point number: ";
        cin >> floatNum;
    } 

However, things get weird with the second array, which is a string type.

    string secondArray[SIZE];

    // `for` loop to get user input
    for (int i = 0; i < SIZE; i++)
    {
        cout << "Enter a name: ";
        cin.ignore();
        getline(cin, secondArray[i]);
    }

My desired output is to print out both arrays using a function call in main() , printParallel(array1, array2, sizeofarray) , simultaneously using a while loop inside a function, which I've attempted here:

void printParallel(double arrayOne[], string arrayTwo[], const int NUM)
{
    int i = 0;
    while (i < NUM)
    {
        cout << arrayOne[i] << " " << arrayTwo[i] << endl;
        i++;
    }

}

But the output is slightly off:

Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
1.23 AA AA
1.23 A AA
1.23 A AA
1.23 A AA
1.23 A AA

Can someone explain what's going on here? The first "A" seems to be overwritten by a null character. Also, is there a better way I should be asking questions in order to solve problems better in the future?

Problem:

cin.ignore(); inside the loop

This is equivalent to cin.ignore(1);

It is ignoring every first character of your string inputs. That is why, all those "AA AA" become "A AA" (the first 'A' is ignored by cin.ignore() ).

Except the first "AA AA" . In this case, cin.ignore() actually ignores the \n after entering the last floating point number from the previous loop. So, the first string output is luckily correct.

Solution:

Call cin.ignore() just once before entering that loop instead of calling it for every single string input.

getline(cin, secondArray[i]); eats the \n at the end of those inputs and you need not cin.ignore() them.

// `for` loop to get user input
cin.ignore(); // solution
for (int i = 0; i < SIZE; i++)
{
    cout << "Enter a name: ";
    // cin.ignore(); <-- Mistake
    getline(cin, secondArray[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM