I'm attempting to make a program that reads the number of lines, words, characters, unique words and unique characters. So far I've gotten everything correct except for the words and characters. The program just reads the words and characters of the last sentence inputted. Any help would be greatly appreciated.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <set>
using std::set;
usingnamespace std;
// write this function to help you out with the computation.
unsignedlong countWords(const string& s, set<string>& wl)
{
string x = " \t";
unsignedlong count = 0;
size_t pos = -1, next = s.find_first_of(x);
while (next != string::npos){
if (pos +1 < next){
++count;
wl.insert(s.substr(pos+1,next-pos-1));
}
pos = next;
next = s.find_first_of(x,next+1);
}
if (pos+1 < s.length()){
++count;
wl.insert(s.substr(pos+1));
}
return count;
}
int main(){
cout << "Please Enter a couple of Words" << endl;
string linecount;
set<string> wordcount;
set<string> linecount1;
unsignedlong lines = 0; // still not certain whats the point of "unsigned" only using it because its in the function above
unsignedlong words = 0;
unsignedlong charcount = 0;
while(getline(cin,linecount)){
++lines;
words =+ countWords(linecount,wordcount);
charcount =+ linecount.length()+1;
linecount1.insert(linecount);
}
cout << "\t" << lines /*"Number of lines: " << lines*/
<< "\t" << words//"Number of words: " << words << endl;
<< "\t" << charcount // << "Number of characters: " << charcount << endl;
<< "\t" << linecount1.size()//"Unique lines: " << linecount1.size() << endl;
<< "\t" << wordcount.size() << endl;//<< "Unique words: " << wordcount.size() << endl;
return 0;
}