When is the right time to use cin.ignore()?

I just wrote a random program using a data structure so I can experiment with some of the functions I had recently learned. But the one function that I don't fully understand is the cin.ignore() function. When do you think is the right time to include cin.ignore()?

The program below will ask you random questions, but in lines 47 and 48 seem to be a little bit buggy. When the structure variable is passed to function1, it counts the vowels in the char array but it doesn't count all of them.

For example: if the sentence has 3 vowels, the function will return 2 vowels.

Of course I know the problem is the "cin.ignore()" I placed in line 47. Once I removed it, the function returned the correct amount of vowels.

But my question is, why did cin.ignore() ignore the last vowel?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

struct Data
{
    int number1;
    int total;
    string number2;
    char cString[20];

}variable1,variable2;

Data function1 (Data x)
{

    x.total = 0;

    cout << "Hello ";
    cout << x.number2 << endl;

    for (int n = 0; n <= x.cString[n]; n++)
        {
        if (toupper (x.cString[n]) == 'A')
            x.total += 1;
        else if (toupper (x.cString[n]) == 'E')
            x.total += 1;
        else if (toupper (x.cString[n]) == 'I')
            x.total += 1;
        else if (toupper (x.cString[n]) == 'O')
            x.total += 1;
        else if (toupper (x.cString[n]) == 'U')
            x.total += 1;
        }

    return x;

}


int main ()
{
    cout << "Hey man, what's your name?" << endl;
    getline (cin, variable1.number2);

    cout << "Please input a sentence so that i can count the vowels in it" << endl;
    cin.ignore();
    cin.getline (variable1.cString, 20);

    cout << "How old are you again?" << endl;
    cin >> variable1.number1;

    variable2 = function1 (variable1);


    cout << variable2.total << " is the number of vowels in the sentence" << endl;

}


istream& ignore (streamsize n = 1, int delim = EOF);
Extract and discard characters
Extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.

http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.