array of 26 ints to hold the character counts

I need to create a program which counts how many times each letter of the alphabet appears in a file. If the character is a letter of the alphabet then I need to increment the appropriate array element. I also need to create a counter for the characters that are non-alphabetic character. I need to display the array elements which shows the character counts and display the non-alphabetic character counter.

This is what I have so far, but can not figure out what to do after this:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;

void cont ();

int main(){
char c;
int ALPHA [26];
int t;

ifstream infile("cities.txt");
if (!infile) {
cout << "Error: Unable to open file\n";
cont();
return 1;
}
//process file
infile.get(c);
while (!infile.eof()) {
cout << c << '\n';
infile.get(c);
}
c = toupper(c);

if (c >= 'A' && c <= 'Z') {


infile.close();



cont();
return 0;
}

void cont() {
if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n');
cout << "Press enter to continue...";
cin.get();
}
After you figured out that "c" is a character, you have to increase the according variable in the array.


ASCII of letter A minus 65 would be 0, so ALPHA[0] would be increased by 1.
Since letter A has a value of 65, you can simply do this (in Pseudo-Code)
ALPHA[ascii-value of variable c minus 65] increase 1

I hope, that helps.

regards

int main
Last edited on
I changed few things around and placed the if statement right after the file finished reading the file. After the if statement that finds out if the character is a letter or not, I am still not sure on what line to write that will increase the proper array. I understand the A = count[0] and B = count[1] and so on.


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;

void cont ();

int main(){
char c;
const int count [26] = {0};
int nonletter = 0;

ifstream infile("cities.txt");
if (!infile) {
cout << "Error: Unable to open file\n";
cont();
return 1;
}
//process file
infile.get(c);
while (!infile.eof()) {
c = toupper(c);
if (c >= 'A' && c <= 'Z'){
count[c]++;
}else {
nonletter++;
}

infile.get(c);
}
cout << "Character counts: " << endl;
for(c=0; c<26; c++){
}
cout << (char)('A' + c) << ": " << count[c] << endl;


infile.close();



cont();
return 0;
}

void cont() {
if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n');
cout << "Press enter to continue...";
cin.get();
}

Thank you for the help
 
count[c]

won't work because letter 'A' equals 65. So, if char c='A' you will increase count[65] instead of count[0].
You must reduce the value of c by 65.

Same goes for your for-loop.

int main
You better use map container..!!
I finally got the program to do what I wanted to do. I subtracted char c with 'A' to do the increment of the array.

Thanks for the help...


int main(){
char c;
//array of 26 ints to hold the character counts
int count [26] = {0};
int nonletter = 0;
// inputs the file and gives a error if unable to locate the file
ifstream infile("cities.txt");
if (!infile) {
cout << "Error: Unable to open file\n";
cont();
return 1;
}
//process the file
infile.get(c);
while (!infile.eof()) {
c = toupper(c);
/*increases the appropriate array element depending wheather
it is a character or not.*/
if (c >= 'A' && c <= 'Z'){
count[c - 'A']++;
}else {
nonletter++;
}

infile.get(c);
}
//Displays the total for each letter and non letter
cout << "Character counts: " << endl;
for(c=0; c<26; c++){
cout << (char)('A' + c) << ": " << fixed << setw(5) << count[c] << endl;
}
cout << "Other characters: " << nonletter << endl << endl;

infile.close();

cont();
return 0;
}
Use a map =\

You can try this code!
///////////////////////==Win Xp + Vc8.0 pass==//////////////////////////
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
/********************************************************************
	created:	2008/07/05
	created:	5:7:2008   1:08
	filename: 	e:\Coding\CSDN_CountCharacter\CSDN_CountCharacter\CSDN_CountCharacter.cpp
	file path:	e:\Coding\CSDN_CountCharacter\CSDN_CountCharacter
	file base:	CSDN_CountCharacter
	file ext:	cpp
	author:		hecan
	
	purpose:	test data file cities.txt:
	==========dalian shanghai beijing===================
*********************************************************************/

#include <iostream>
#include <fstream>
#include <map>
#include <cstdlib>

using namespace std;

int main()
{
	ifstream ifs_Data("cities.txt");
	if (!ifs_Data)
	{
		cerr << "Error!" <<endl;
	}
	char cData;
	map< char, int > map_Data;
	while ( ifs_Data >> cData )
	{
		if (isalpha(cData))
		{
			++map_Data[cData];
		}
	}
	for (map< char, int >::iterator mci_Iter = map_Data.begin(); mci_Iter != map_Data.end(); ++mci_Iter)
	{
		cout << mci_Iter->first << " " << mci_Iter->second <<endl;
	}

}
Topic archived. No new replies allowed.