Input/Output Text Files

My assignment reads like this:
1)Create an array of 26 components to do the letter count for the 26 letters in the alphabet and a variable for the line count, (You can call these variables letterCount and lineCount respectively).

2)Hint: Use the plan of declaring variables, opening input and output files, initializing the variables, reading and writing the character for each character in a line, incrementing the letter count, incrementing the line count, outputting line and character counts and, finally, closing the input and output files.

3)Create a text file that contains the text that will be used as input to your program. Call this file text input.

4) Have the output stored in a file called textoutput.

5)Create ifstream and ofstream objects called "infile" and "outfile" respectively.

I am still a little confused with items 2 and 5. I am not sure how to add a lineCount function into this program. Here is what I currently have for code. Any help would be appreciated!!!

#include
#include
#include
#include

const int SIZE=26;
void PrintOccurences (const int[]);//prototype
using namespace std;

int main()
{
ifstream textinput;
int letterCount;
char ch;
char index;
textinput.open ("textinput.txt");
if (!textinput)

{
cout<<"Cant Open Input File!"<< endl;
return 1;
}

for (int m=0; m<SIZE; m++) //zero out the array
letterCount [m]= 0;
textinput.get (ch); //priming read
while (textinput) // while last read was successful

{
if (isalpha (ch))
{

if (islower (ch))
ch= toupper (ch);
letterCount[ch - 'A'] += 1;;
}

textinput.get (ch); //get next character
}

PrintOccurences (letterCount);
return 0;

}

void PrintOccurences ( const int letterCount [])

{
char index;
cout<<"textinput.txt"<<endl;
cout<<"Letter Occurences"<<endl;
for (index='A'; index<='Z'; index++)
{
cout<< setw(4)<<index<<setw(10);
cout<<letterCount[index - 'A']<<endl;
}
}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories