cin problem

why the second cin didn't execute?


#include<iostream>
#include<algorithm>
#include<string>
#include<list>

using namespace std;

int main()
{
string s;
list<string> l;
cout << "input string:" << endl;
while (cin >> s)
l.push_back(s);
cout << "input the string will be found:" << endl;
cin.ignore(1024);
string str;
cin >> str;
auto n = count(l.begin(), l.end(), str);
cout << str << " " << "occurs" << " " << n << " " << "times!" << endl;

return 0;
}
1
2
while (cin >> s)
l.push_back(s);


Should be :
1
2
if (cin >> s)
l.push_back(s);


However, you can just press Ctrl + Z to forcefully break the while loop. These lines will be needed later.

1
2
cin.clear();
cin.ignore();
Topic archived. No new replies allowed.