Printing number of lines, words, char, etc in a string

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
Last edited on
Anyone??
Hello everyone. After many tutorials online, readings, etc. I've got it working! =D
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
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cctype>
#include <sstream>

using namespace std;

// write this function to help you out with the computation.
unsigned long countWords(const string& s, set<string>& wl);
int charAmount = 0;
int main()
{
	int numOfLines = 0, totalWords = 0;
	string input;
	set<string> nwords, lines; //2 sets
	while (getline(cin, input))
	{
		lines.insert(input);
		numOfLines++; 			// COUNTS NUMBER OF LINES
		charAmount += input.length(); 	//COUNTS NUMBER OF CHARACTERS
		totalWords += countWords(input, nwords); //COUNTS NUMBER OF WORDS
		//lines.size() prints out the unique lines stored in the set //
		//nwords.size() prints out the words that are unique in the set //
	}
	cout << numOfLines << '\t' << totalWords << '\t' << charAmount << '\t' << lines.size() << '\t' << nwords.size() << '\t' << endl;
	system("pause");
}

unsigned long countWords(const string &s, set<string> &wl)
{
	int count = 0;
	string inputCounter;
	stringstream ss(s);
	while (ss >> inputCounter) {
		wl.insert(inputCounter);
		++count;
	}
		for (int sc = 0; sc < s.size(); sc++) {
			if (s[sc] == '\t' && s[sc] == '\n' && s[sc] == ' ')
				charAmount++;
	}
	return count;


Now, the problem is the professor designed a script to test if the program is working correctly. The program isn't counting new lines or tabs in character amount, but its counting spaces. What am i doing wrong?
Topic archived. No new replies allowed.