Query: Why getline() is not stopping for user input as per current code?
// Program to find the word count in the string with input words array
#include <iostream>
#include <string>
#include <map>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<string> arr;
string individualString;
string str;
map<string, int> wordCount;
map<string, int>::iterator itr;
int numberofWords;
cout << "Enter the number of words :" << endl;
cin >> numberofWords;
cout << "Enter the Words" << endl;
for (int i = 0; i < numberofWords; i++)
{
cin >> individualString;
arr.push_back(individualString);
}
cout << "Enter the String to be searched for Words" << endl;
getline(cin, str);
//wordCount = prepareWordsCountMap(arr, numberofWords);
//wordCount = countWordsInString(str, wordCount);
return 0;
}
NOTE: If I move this
getline(cin, str);
just after all variable declarations, it works fine and accepts the input string from user.