Letter and Word Project Output Error

Hello, I was just finishing up a project and though it compiled I seem to be getting a logic error. What I am trying to do is write something similar to the wc unix command so that the program will count how many words and letters are in a file. A sample of the output I want would be as follows-

words.txt consists of the following text-

Now is the time for all good men to come to the aid of their party

And the output should look like

16 words
3 a
1 c
2 d
6 e
2 f
1 g
3 h
4 i
2 l
3 m
2 n
8 o
1 p
3 r
1 s
7 t
1 w
1 y



instead I am recieveing

1 words
1 {



Can anyone show me what I have done incorrect? I double checked my code and words.txt file and both are accurate to the best of my ability.

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
#include <iostream>
#include <fstream>
using namespace std;
int main()
{

int in_word = false;
int word_count = 0;
char ch;
char low_case;
int char_count[26];
int i;

ifstream fin;
fin.open("words.txt");

for (i = 0; i < 26; i++)
        char_count[i] = 0;

while( '\n' !=(ch=fin.get()))
{
        if(' ' == ch || '\n' == ch || '\t' == ch)
                in_word == false;
        else if(in_word == false)
        {
                in_word = true;
                word_count++;
        }
        low_case = tolower( ch);
        char_count[ int(low_case) - int('a') ]++;
}

cout << word_count << " words" << endl;
        if(char_count[i] !=0)
                cout << char_count[i] << " " << char (i + 'a') << endl;

return 0;
}




Thanks in advance
Last edited on
OK I figured it out without any input(at this point)
I had forgotten to add a for loop at line 34...
I am having the same exact problem. Would you post the loop that you used to make this work? Thanks!

Edit: Never mind, got it to work!
Last edited on
Topic archived. No new replies allowed.