c++ program that count the occurrence of character in a text file and produce a histogram of the character count.

Here 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
33
34
35
36
37

#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int sum=0; 
	int count [26];
	char ch;
	ifstream inFile; 
    string fileName;
    string word;
    
	inFile.open("input any text file in");
while (inFile >> ch){

  if (isalpha(ch)) {
   ch = tolower(ch);
   count[ch - 'a']++;
  }
 
 while (inFile >> ch){

  if (isalpha(ch)) {
   ch = tolower(ch);
   count[ch - 'a']++;
  }
 }

    cout << "Number of words in file is " << count;
    inFile.close();

    cin.get();  
    return 0;
}

I also need to do a loop that scan the count array and check if the element is bigger than the previous one.
1. Why do you need this second while-loop? Its body will never be executed because its condition initially fails.

2. Your final cout-statement will only print the pointer value of count pointing to an array of ints. You should better loop over the array count printing its 26 values.
Topic archived. No new replies allowed.