Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a given text file, outputs the text file as is, and a

ID: 3626665 • Letter: W

Question

Write a program that reads a given text file, outputs the text file as is, and also prints the number of lines and the number of times each letter appears in the text file. (To simplify things, treat uppercase letters and lowercase letters as being the same.)
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 may wish to call these variables letterCount and lineCount respectively.)
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 letter counts and, finally, closing the input and output files. (You may wish to call the program outputting line and letter counts writeTotal.)
Create an ASCII (or text) file that contains text that will be used as input to your program. Call this file textinput.
Have the output stored in a file called textouput.
Create ifstream and ofstream objects called "infile" and "outfile" respectively.
Include 4 functions for initializing, copying text, counting, and writing the results to a file, respectively.
Initialize the initial array with 0's.
Have the file copying text specifically be able to read a line and output that line. (You may wish to call this file copyText.)
Whenever a nonblank character is found, it calls the function that does the counting, characterCount, to update the letter count. (Do not count blank, tab, or end-of-line characters as part of the letter count.)
Have the characterCount function convert the letter to uppercase, find the index of the array corresponding to this letter and, if the index is valid, increment appropriate count.
Have the main program do the following:
Declare variables.
Open the input and output files.
If the input file does not exist, exit the program.
Open the output file.
If the output file cannot open, exit the program.
Initialize variables counting the lines and letters.
Read the first character.
Use while (not end of input file).
Process the next line using the function that reads a line and outputs the line copyText.
Increment the line count. (Increment the variable lineCount.)
Read the next character.
Output the line count and letter counts.
Use writeTotal.
Close files.



This is the question I got asked. I have gotten this so far:

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{

int lineCount = 0;
int letterCount[26];

for(int i = 0; i < 26; i++)
letterCount[i] = 0;

ifstream infile;
infile.open("textinput.txt", ios::in);

if(!infile)
{
cerr<<"File does not exist."<<endl;
exit(1);
}

ofstream outfile;
outfile.open("textoutput.txt", ios::out|ios::binary);

if(!outfile)
{
cerr<<"File cannot be opened."<<endl;
exit(1);
}
char data[100];
outfile<<data;

while(infile>>data)
{
outfile<<data<<endl;
}

while(infile)
{
char ch1 = infile.get();
if(ch1 == ' ')
{
lineCount++;
continue;
}

int asciiNum = (int)ch1;
if(asciiNum > 96)
{
asciiNum = asciiNum - 97;
}
else
{
asciiNum = asciiNum - 65;
}

letterCount[asciiNum]++;
}


infile.close();
outfile.close();
system("PAUSE");
return 0;
}


So far the program runs but the output file has all the words but the punctuation and blank spots became weird characters. And I dont know how to output the array to show the count for each letter. If you could help me solve those two problems I would be very appreciative.

Explanation / Answer

Hey the code is in the link: http://codepad.org/uQ6fQxG8 I really didnt fix much, just fixed some logic errors such as the if statement for asciiNum which i left comments on. overall very good program, just remember you can only read in a line from a file once, so you want to do everything you can with that line before you read in a new line. I also compartmentalized it to almost the requirements of what your teacher said, left out the read in line problem, since it really isnt necessary for two read line statements. Enjoy and please Rate ~Spaction