The program is suppose to read from a text and count each letter of the alphabet and the counter_other counts everything else except whitespace. I'm having trouble putting this together, especially the switch. We have not gone over strings yet in class so I don't think I'm suppose to use it in the program.

#include<iostream>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;
void readData(istream &, int []);
void initialize(int[], int);
void printResults(ostream &, int []);
const int SIZE= 27;
int  main()
{
     int letterCount[SIZE];
     char index;
     ifstream infile;
     ofstream outfile;

     initialize(letterCount, SIZE);
     readData(infile, letterCount);
     printResults(outfile, letterCount);



     infile.close();
     outfile.close();




system("pause");
return 0;
}

void initialize(int letterCount[], int n)
{
     for(int i=0; i<SIZE; i++)
     letterCount[i] = 0 ;
}


void readData(istream &in, int letterCount[])
{
     int counter=0;
     char index;
     index = in.get();
     in>>index; // priming read
         while(index) // while last read was successful
         {

                in>>index; // get next character       
                  if(index=0; index<27; index++)   

                       case 'A': counter[0]++; break;          
                       case 'B': counter[1]++; break;           
                       case 'C': counter[2]++; break;           
                       case 'D': counter[3]++; break;            
                       case 'E': counter[4]++; break;           
                       case 'F': counter[5]++; break;            
                       case 'G': counter[6]++; break;            
                       case 'H': counter[7]++; break;            
                       case 'I': counter[8]++; break;            
                       case 'J': counter[9]++; break;            
                       case 'K': counter[10]++; break;            
                       case 'L': counter[11]++; break;            
                       case 'M': counter[12]++; break;            
                       case 'N': counter[13]++; break;            
                       case 'O': counter[14]++; break;            
                       case 'P': counter[15]++; break;            
                       case 'Q': counter[16]++; break;            
                       case 'R': counter[17]++; break;            
                       case 'S': counter[18]++; break;            
                       case 'T': counter[19]++; break;           
                       case 'U': counter[20]++; break;            
                       case 'V': counter[21]++; break;            
                       case 'W': counter[22]++; break;            
                       case 'X': counter[23]++; break;            
                       case 'Y': counter[24]++; break;            
                       case 'Z': counter[25]++; break;           
                       default : counter[26]++; break;

         }
     return;

}  


void printResults(ostream &out, int letterCount [ ] )// Prints each alphabet character and its frequency
{ 
     ofstream outfile("C:/Users/gina 0/Documents/histogram.txt");
     outfile<< " LETTER "<< " OCCURRENCES "<< " PERCENTAGE " <<endl;
     for (int i = 0; i < 26; i++)   
      {     
         if (letterCount[i] > 0)    
        {     
             outfile << letterCount[i] << " " << char('A' + i) << endl;
        }
     outfile.width(15);
     }
     outfile.close();

     return;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This is a very simple programming problem which can be done with only 8 lines of code! All you have to do is create an array of 255 ints then use each letter of the string as an index into the array. So if you have 'A' then all you have to do is increment counts. Yes, you do not really need all 255 array elements, but it makes the program a lot more efficient if you use it.

The instructions say you only count the number of alphabetical characters -- 'A'-'Z' and 'a'-'z'. The macro isalpha will help you do that

// assuming a variable named string
if( isalpha(string[i]) ) 
   // increment the counter
   counts[string[i]]++;

Finally, to print out the results, just loop through the array and for those values greater than 0 print the value of the loop counter cast as char, and the value of the counters array.

Edited 7 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.