In: Computer Science
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 have worked 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.
How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be appreciated.
This is the original code that makes it make a new line after every carriage return. I need it to make a new line after every sentence.
Here is the input text file :
Today we live in an era where information is processed
almost at the speed of light. Through computers, the
technological revolution is drastically changing the way we
live and communicate with one another. Terms such as
“the Internet,” which was unfamiliar just a few years ago,
are
very common today. With the help of computers you can send
letters to, and receive letters from, loved ones within
seconds. You no longer need to send a résumé by mail to apply
for a job; in many cases you can simply submit your job
application via the Internet. You can watch how stocks
perform
in real time, and instantly buy and sell them. Students
regularly “surf” the Internet and use computers to design
their classroom projects. They also use powerful word
processing software to complete their term papers. Many
people maintain and balance their checkbooks on computers.
Sorry the last question i posted looked very ugly!!
//*************************************************************
// 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.
//*************************************************************
#include
#include
#include
using namespace std;
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
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outfile.open("textout.out"); //Step 4
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast(ch)
- static_cast('A'); //Step b
if (0 <= index && index < 26) //Step c
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[])
{
int index;
outtext << endl << endl;
outtext << "The number of lines = " << lc <<
endl;
for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal
This is the code I've got so far to make it ask for what the output file should be named.
//*************************************************************
// 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.
//*************************************************************
#include
#include
#include
using namespace std;
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;
string fileName;
infile.open("textin.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
cout << "Please enter the name of the output file: ";
<----- This is the new code
getline(cin, fileName); <----- This is the new code
outfile.open(fileName.c_str()); //Step 4 <----- This is the new
code
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast(ch)
- static_cast('A'); //Step b
if (0 <= index && index < 26) //Step c
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[])
{
int index;
outtext << endl << endl;
outtext << "The number of lines = " << lc <<
endl;
for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal
Given below is the modified code.
//*************************************************************
// 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.
//*************************************************************
#include <iostream>
#include <fstream>
//#include
using namespace std;
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;
string fileName;
infile.open("textin.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
cout << "Please enter the name of the output file:
";
getline(cin, fileName);
outfile.open(fileName.c_str()); //Step 4
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (!infile.eof()) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast<int>(ch)
- static_cast<int>('A'); //Step b
if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount
void copyText(ifstream& intext, ofstream& outtext,
char& ch,
int list[])
{
while (!intext.eof() && ch != '.') //process the entire
sentence
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext<< ch << endl << endl; //output 2 newline
character
} //end copyText
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;
} //end writeTotal