c++, need help on code?

Sam Weaver

New member
trying to write a program that takes a paragraph and shows how many times each letter in the alphabet is used, right now it outputs it to a file, It's not reading the paragraph in the file thus its not working. Also want to change it so it outputs the characters on the screen ie: A=14 B=12 C=2 etc...instead of in a file. Please help me wizards of the internet.

Here is the code.

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void charachterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("c:\documents and Settings\Dr Manchild\Desktop\ass8.txt");
if (!infile)
{
cout << "File cannot be opened" << endl;
return 1;
}
outfile.open("c:\Documets and settings\Dr Manchile\Desktop\ass8out.txt");

initialize(lineCount, letterCount);

infile.get(ch);

while (infile)
{
copyText(infile, outfile, ch, letterCount);
lineCount++;
infile.get(ch);
}

writeTotal (outfile, lineCount, letterCount);

infile.close();
outfile.close();
return 0;
}



void initialize(int& lc, int list[])
{
int j;
lc=0;

for(j = 0; j < 26; j++)
list[j]=0;
}

void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch);

index= static_cast<int> (ch)
- static_cast<int>('A');

if (0 <= index && index < 26)
list[index]++;
}

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')
{
outtext << ch;
characterCount(ch, list);
intext.get(ch);
}
outtext << ch;
}

/*void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch);

index= static_cast<int> (ch)
- static_cast<int>('A');

if (0 <= index && index < 26)
list[index]++;
}*/

void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast<char> (index + static_cast<int>('A'))
<< " count = " << list[index] << endl;
}
 
Back
Top