I am going to say this outright this is an assignment for school and my teacher has said i can come in for help but i do not have any transportation due to hurricane harvey. that being said i need to edit this code here to do the following(in teachers words not mine):
We will continue working with the Letter Counting Program from last week. (Just when you thought it was safe to go back in the water!)
1. First I want you to modify the program to prompt the user for the name of the file to output.
2. Then, change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must work through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE.
3. Also, it would be nice if you could shave off any unnecessary spaces (such as those before the sentence begins).
7. Finally, don't let the screen just flash by - add the code to tell the user what is happening.
Take your time with this program and get it right. As I mentioned before, there is MUCH to be learned from it!
I need help with steps 2 and 3 above I have the rest covered Thanks in Advance!
//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************
void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);
int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
infile.open("textin.txt"); //Step 2 Attempts to Open Desired file
if (!infile) //Step 3 If program cannot find/open file it outputs "Cannot open the input file."
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outfile.open("textout.out"); //Step 4 Converts the file to write to and output
initialize(lineCount, letterCount); //Step 5 Calls on function initialize to initialize linecount and lettercount
infile.get(ch); //Step 6 reads and stores characters in ch
while (infile) //Step 7 while function that copies infile to outfile
{
copyText(infile, outfile, ch, letterCount); //Step 7.1 Copies data between in/outfiles and reads characters and counts the characters
lineCount++; //Step 7.2 Counts lines
infile.get(ch); //Step 7.3 reads and stores characters in ch
}
writeTotal(outfile, lineCount, letterCount); //Step 8 writes to the outfile to be shown in text.out
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a Makes each individual letter A capital letter and counted
index = static_cast<int>(ch)
- static_cast<int>('A'); //Step b gives each letter a value
if (0 <= index && index < 26) //Step c index's letters to be called in write total
list[index]++;
} //end characterCount
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
void writeTotal(ofstream& outtext, int lc, int list[]) // writes totals at bottem of page
{
int index;
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;
Teacher says im overthinking things by wanting to add another while statement and trying to get the ., !, ? read differently but does not give me anything to go on do i just add to the existing while statement?
Well this is where you might go back to reading the file character by character. You have to count characters anyway. I’ll give you a couple of tips.
1. If you have a character char x. x is a period if x==‘.’
2. Checkout noskipws as a function so when you read character by character spaces aren’t ignored.
3. A sentence is made up of an array of all characters, including spaces with a period and ‘\0’ terminator at the end. You can build up the sentence character by character.
See how you go. Use lots of cout’s to track your progress by modifying the above.
So what you are saying is i can use linecount to count the empty spaces after the peroid and use cout << linecount << endl << endl; but wouldnt it count the end of the line and not after the period?
Every sentence has a period at its end (denoted ‘.’). Every line has a carriage return (denoted ‘\r’ )
My bad for mistakenly focussing on counting lines.
All you have to do is read the input file character by character and write each character to the output file - ie copy the input file - with two exceptions. First, if an existing carriage return is encountered than ignore it, don’t copy it. Second, if a period is detected then copy the period across but add two carriage returns.
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch == '.') // added code
{
cout << endl << endl;
} // end added code
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
or did i edit the wrong function or should i nest it somehow?
As far as your code is concerned you only need one while loop and two if’s inside. The while is there to read the file until the end, ie no more characters. The if’s to do the sentence/paragraph/carriage return stuff
1 2
loop while characters to read
if character is a period
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
if (ch == '.')
cout << endl << endl;
if (ch == '\n')
cout << "\b";
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
im not sure how not to read the \n character any ideas or will the \b work here?
Well the test is really, what did you get when you run it?
However, you might like to consider
You now have to write some code for the general case and the other case I mentioned where it is not the end of a sentence but the end of the line.
Don’t forget to print stuff out to display the results of what you‘re doing. You can even add a message cout << “Bingo period encountered\n”; in that if.