Hi,
I would like to get some suggestions on how to change this program from array to vectors. This program reads an ASCII input file and outputs the character frequency into a text file. I got it to work with the arrays, but I can't figure out where to do the changes so that an vector can accomplish the same thing.
Thank you.
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
//declare void functions
void initialize (int& lc, int set[]);
void readText(ifstream& intext,ofstream& outtext, char& ch, int set[]);
void countCh (char ch, int set[]);
void totalCh(ofstream& outtext, int lc, int set[]);
int main ()
{
//declare variables
int lineCounter; //variable to sotre the line count
int asciiCount; //array to store letter count
char ch; //varaible to store a character
ifstream infile; //input file stream variable
ofstream outfile; //output file stream variable
// check for file
infile.open ("textIn.txt");
if (!infile)
{
cout << "Cannot open the input file." << endl;
return 1;
}
outfile.open("textOut.txt");
//call functions
initialize (lineCounter, asciiCount);
infile.get(ch);
readText(infile, outfile, ch, asciiCount);
totalCh (outfile, lineCounter, asciiCount);
cout << endl;
infile.close();
outfile.close();
return 0;
}
void initialize (int& lc, int set[])
{
int j;
lc = 0;
for (j = 0; j < 127; j++)
set [j] = 0;
}
void readText(ifstream& intext,ofstream& outtext, char& ch , int set[])
{
while (intext) //process the text
{
countCh (ch, set); //call function count character
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
}
void countCh (char ch, int set[])
{
int index;
index = static_cast<int>(ch) - static_cast<int>('A');
if (0 <= index && index < 127)
set[index]++;
}
void totalCh(ofstream& outtext, int lc, int set[])
{
int index;
outtext << endl;
outtext << "The number of characters in this file are: " << endl;
outtext << endl;
for (index = 0; index < 127; index++)
outtext << static_cast<char>(index + static_cast<int> ('A')) << " count = " <<set[index] << endl;
}