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;
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.
#include <iostream>
#include <string>
#include <vector>
usingnamespace 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?