Getline makes you enter phrase twice

When I use this code:
1
2
3
4
5
6
7
8
9
10
int main()
{
     string house;
     char found;

     getline(cin, house);
     cin.ignore();
     found = house.find("storage");
     cout << int(found);
}

It makes you input the phrase twice before it displays the position.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>

using namespace std;

int main()
{
     string house;
     char found;
     getline(cin, house);
     cout << "Done getline..." << endl;
     cin.ignore();
     cout << "Done ignore..." << endl;
     found = house.find("storage");
     cout << int(found);
}

Last edited on
I have modified it to:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
     string house;
     char found;

     getline(cin, house);
     cout << "TEST\n";
     cin.ignore();
     found = house.find("storage");
     cout << int(found);
}

And it prints test after you put the input, but it makes you press a key to continue. Why is that? Ex. If I put the phrase, "storage rocks", and then press enter, it will print test but I have to hit enter again for it to continue. Why is that?
Last edited on
Because of this:
cin.ignore();

Topic archived. No new replies allowed.