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 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++;
}
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;