For a school project I have to write a program that reads inputs and when ended (using CTRL + D) outputs the line count, total words, character total, and unique lines.
For example, inputting:
hello
should output:
1 , 1, 5, 1 (1 line, 1 word, 5 characters, 1 unique line)
The problem is this: Currently my program is outputting all of these numbers correctly EXCEPT for the word count.
In my program the above would output:
1, 0 , 5, 1
Here is my code:
Thanks in advance for your help. I am a beginner student so while there are easier ways of doing this, I'd prefer a fix that fits with my code.
For example, inputting:
hello
should output:
1 , 1, 5, 1 (1 line, 1 word, 5 characters, 1 unique line)
The problem is this: Currently my program is outputting all of these numbers correctly EXCEPT for the word count.
In my program the above would output:
1, 0 , 5, 1
Here is my code:
int main() { string input; int i; //Used in FSM int wordsperstring = 0; int wordstotal= 0; int charactertotal= 0; int linecount= 0; char c; //Used in FSM set <string> uniquewords; set <string> uniquelines; int state = 0; //Used in FSM while (getline(cin, input)) { uniquelines.insert(input); linecount++; charactertotal = charactertotal + input.size(); // FSM starts here: for (i = 0; i < input.size(); i++) { c = input[i]; if (state == 0 && c != ' ' && c != '\t') state = 1; else if (state == 1 && c != ' ' && c != '\t') state = 1; else if (state == 1 && c == ' ' || c == '\t' || c == '\n') { state = 0; wordsperstring++; wordstotal = wordstotal + wordsperstring; } } } cout << linecount << "\t" << wordstotal << "\t" << charactertotal << "\t" << uniquelines.size() << "\t" << uniquewords.size() << endl; return 0; }
Thanks in advance for your help. I am a beginner student so while there are easier ways of doing this, I'd prefer a fix that fits with my code.