Count all the printable characters in a text file

I'm writing a program that counts each printable character (ASCII 32-126)using an array and outputs the count for each character. The output will only be the characters found in the text file. Any tips?

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;


int main()
{
	char ch;
	int i=0, count[94];
	string fname;
	ifstream infile;

	cout<<"Please enter file name: ";
	cin>>fname;

	for (int i=0; i < 94; i++)
	{
		count[94]=0;
	}

	
	infile.open(fname);

	while (!infile.eof())
	{
		infile.get(ch);

		if (ch >= 32 && ch <= 126)
		{
			count[94]++;
		}
	}

	infile.close();


return 0;
}
Last edited on
First of all, you're accessing out-of-bounds elements on lines 21 and 33.
Using .eof() on ifstream objects is iffy, and will usually not perform the way you want or expect it to.
Topic archived. No new replies allowed.