I am trying to write write a program that reads a text file, counts each unique word, and then sorts the list of unique words and lists the number of occurrences of each word. However, I cannot seem to read in a single word from a string without messing up and reading in letters, numbers, and symbols. I've read other topics, but my logic is severely flawed in some way that I don't see.
int main()
{
fstream fp;
string line;
fp.open("syllabus.txt", ios::in);
getline(fp, line);
string word = findWords(line);
cout << word << endl;
}
string findWords(string &line)
{
int j = 0;
string word;
for(int i = 0; i < line.size(); i++)
{
while(isalpha((unsigned char)line[j]) != 0 && isdigit((unsigned char)line[j]) != 1)
j++;
word += line.substr(0, j) + " + ";
line = line.substr(j, (line.size() - j));
}
return word;
}