I am trying to input all words into a map in C++, but the program freezes only when a word starts with a special character. The code works when there is a special character at the end.
I haven't been able to find proper documentation for the >> operator in C++, or been able to google my problem properly.
//Map values and find max value
//The code works for all words except the ones that start with special characters
while(myFile >> cWord){
//put the characters into a string
//DEBUG: cout << "real word: " << cWord << " | ";
cWord = stripWord(cWord);
//delete common words before they're in the system
if(cWord == "a" ||
cWord == "an" ||
cWord == "and" ||
cWord == "in" ||
cWord == "is" ||
cWord == "it" ||
cWord == "the"){
continue;
}
if (wordMap.count(cWord) == 0){
wordMap.insert({cWord, 1});
}
else{
wordMap[cWord]++;
if(wordMap[cWord] > maxWordRep){
maxWordRep = wordMap[cWord];
}
}
//DEBUG: cout << cWord << " | " << wordMap[cWord] << endl;
}
I expect the debug to print all of the words and then follow the rest of the code, but the code stops running and freezes at the exact line
while(myFile >> cWord)
My input are long song lyric files. Here are the words that program froze on:
Counting Stars: Completed.
I can make your hands clap: Stuck at 'cause
One more night: Stuck at (yeah
Run on test (a file to test combined words): Completed
Safety Dance: Stuck at 'em
Shake it off: Stuck at "oh
There are a bunch of others that follow the same pattern. Always 1 or more special characters in front. You can try on your own, and cin >> string will get stuck when you input a string with a special character in front.
Final Edit: The bug was in the stripWord function, so this question is just a bad question.