Hello everyone. I've been assigned to create a program to print out the number of lines, words, characters, unique lines(lines that dont repeat), and unique words(words that dont repeat).
Now this is what I have so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cctype>
using std::strcat;
using namespace std;
/*unsigned long countWords(const string& s, vector<string>& wl); */
int main()
{
int charAmount = 0, numOfLines = 0, total = 0, count = 1;
string words;
vector<string> nwords; //Normal words
vector<string> uwords; //Unique words
while (getline(cin, words))
{
nwords.push_back(words); //Adds words to vector
numOfLines++; //Increases line amount
charAmount += words.length();
for (int i = 0; i < words.length(); ++i)
if (words[i] == ' ')
++count;
}
cout << numOfLines << '\t' << charAmount << '\t' << count << endl;
system("pause");
}
|
So far I can obtain the number of lines and the amount of characters (he said its ok for it to count spaces). However, I do have a function there that's checking for spaces through the string, if it does it adds +1 to count. So if I inputed
Hello world today
It would give back 3. It's testing for the amount of words if you haven't gotten it.
The problem I'm having is if I input more then one sentence, the count ignores the sentences after the 2nd one, and sometimes gets the answer off by +1 or -1.
The other is the thing he gave us that unsigned long, but I'm confused on what I'm suppose to do with it. Is it suppose to tie in with the unique line or word. I'm assuming i have to do something along the lines of checking if one string is = to the next. Any help be appreciated. It's due in two days. I have to go to bed now so I'll read all your replies tomorrow :)
Also I know i have some random #includes in there, was from video tutorials i was watching today, didn't clean them up