/* asst05.cpp 7/6/2005 CIS 150 David G. Klick Solution to assignment #5. */ #include #include #include #include using std::cout; using std::setw; using std::ifstream; int main(void) { int count[26] = {0}; int other = 0; char ch; int i; // try to open file, exit if error ifstream infile("cities.txt"); if (!infile) { cout << "Error: Could not open input file\n"; return 1; } // use priming read to start reading from file infile >> ch; while (infile) { // convert character to uppercase ch = toupper(ch); if (ch>='A' && ch<='Z') { // increment array accumulator if letter count[ch-'A']++; } else { // increment other accumulator if non-letter other++; } // try to read next character in file infile >> ch; } infile.close(); // display results cout << "Character counts:\n"; for (i=0; i<26; i++) { cout << (char)(i+'A') << ": " << setw(5) << count[i] << '\n'; } cout << "Other characters: " << other << '\n'; return 0; }