Why does it skip my 'getline'?

I'm trying to make a menu with multiple choices, but when it get's to my getline(cin, Bar) it just skips this and jumps to the next line in code.

What am I doing wrong?

Example:
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
class myClass
{
    private:
        
        string Foo;    // Foo is used as input value for my container
                           // which is not shown in this example.

    public:

        void myFunc(vector<Foobar> &myContainer)
        {
            string Bar;

            cout << "Instructions for the user..." << endl
                 << "                            " << endl;

            getline(cin, Bar);    // THIS LINE: Doesn't shows up in console?

            for(int i=0; i!=myContainer.size(); i++)
            {
                if(myContainer[i].Foo == Bar)
                {
                    myContainer.erase(myContainer.begin()+i,
                                      myContainer.begin()+1+i);
                    break;
                }
            }
        }

....

};

int main()
{
    vector<Foobar> myContainer;

    ....

    // Following takes place in a switch (my menu)

    myClass Baz;

    Baz.myFunc(myContainer);

    ....

return 0;
}


EDIT:
Updated to represent the acutal code. Missed out on myClass Baz;
Last edited on
The function myFunc is a class function of the class myClass. I don't see that you create any objects of that type.

Is this Foobar Baz; meant to be myClass Baz;? Please present actual code.
Last edited on
@Moschops

Please present actual code


It's to long to be presented here, sorry. I did however make the example look like the actual code in the sections where it's messed up =)

The variable names are just made up for the example and not the actual names in my code.
This problem is very common when using operator>> and getline in the same program. When you enter data and press enter that will add a newline character '\n' at the end of the input. operator>> will read the input but leaves the newline in the stream. getline reads until it finds a newline character. The first thing it finds is the newline so it returns right away, making the string variable empty.

The solution is to remove the newline character before calling getline. You can use cin.ignore(); to remove a single character. If you don't know if there is more garbage on before the newline character you can usecin.ignore(numeric_limits<streamsize>::max(), '\n' ); to the whole line.
The following code works fine. It waits for the user to type something in at the line getline(cin, Bar);

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Foobar
{
string Foo;
};

class myClass
{
    private:
        
        string Foo;    // Foo is used as input value for my container
                           // which is not shown in this example.

    public:

        void myFunc(vector<Foobar> &myContainer)
        {
            string Bar;

            cout << "Instructions for the user..." << endl
                 << "                            " << endl;

            getline(cin, Bar);    // THIS LINE: Doesn't shows up in console?

            for(int i=0; i!=myContainer.size(); i++)
            {
                if(myContainer[i].Foo == Bar)
                {
                    myContainer.erase(myContainer.begin()+i,
                                      myContainer.begin()+1+i);
                    break;
                }
            }
        }

};

int main()
{
    vector<Foobar> myContainer;

    

    // Following takes place in a switch (my menu)

    myClass Baz;

    Baz.myFunc(myContainer);

    
return 0;
}


Can you present a minimal complete section of code that demonstrates the problem?
Last edited on
Topic archived. No new replies allowed.