12.7: Sentence Filter
Write a program that asks the user for two file names. The first file will be opened for input and the second file will be opened for output. (It will be assumed that the first file contains sentences that end with a period.) The program will read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file.
Prompts And Output Labels.
Input Specification.
// Function prototype
void capitalize(char *, bool &);
int main()
{
// Constant for array size
const int SIZE = 81;
ifstream inFile; // Input file object
ofstream outFile; // Output file object
string inName; // Input file name
string outName; // Output file name
char inputLine[SIZE]; // Line of input
bool midSentence = false; // Flag, initialized to false
// Get the name of the input file.
cout << "Enter the input file name "
<< "(sentences.txt): ";
getline(cin, inName);
// Open the input file.
inFile.open(inName);
// Test for errors.
if (!inFile)
{
cout << "Error opening "
<< outName << endl;
exit(EXIT_FAILURE);
}
// Get the output file name.
cout << "Enter the output file name: ";
getline(cin, outName);
// Open the output file.
outFile.open(outName);
// Test for errors.
if (!outFile)
{
cout << "Error opening "
<< outName << endl;
exit(EXIT_FAILURE);
}
// Read and process the input file.
while (!inFile.eof())
{
char *ptr;
// Read the string from inFile.
inFile.getline(inputLine, 81, '\n');
// Capitalize the string.
capitalize(inputLine, midSentence);
// Display the line.
cout << inputLine << endl;
// Make ptr point to first character
// in inputLine
ptr = inputLine;
// Write the string to outFile
while (*ptr != 0)
{
outFile.put(*ptr);
ptr++;
}
}
// Close the files.
inFile.close();
outFile.close();
return 0;
}
// Function capitalize
void capitalize(char *str, bool &midSentence)
{
bool period; // Flag
// If midSentence is true then we just processed
// a character other than a period, and we are
// not at the beginning of the sentence. We
// set the period flag to false to indicate
// that we have NOT just encountered a period.
if (midSentence)
period = false;
else
period = true;
// Step through the string.
while (*str != 0)
{
// Skip any leading spaces
while (*str != 0 && isspace(*str))
str++;
// If we have just encountered a period
// and we aren't at the end of the
// string, then str points to the first
// character of a sentence.
if (period && *str != 0)
{
// Convert the character pointed to
// by str to uppercase.
*str = toupper(*str);
// Indicate that we did not just
// encounter a period.
period = false;
// Increment str to the next character.
str++;
}
// Force the rest of the word to lowercase.
while (*str != 0 && !isspace(*str) &&
*str != '.' )
{
*str = tolower(*str);
str++;
}
// If str points to a period, indicate so
// and increment str.
if (*str == '.')
{
period = true;
str++;
}
}
// If the last character we encountered was a
// period, then we are not at midsentence.
// Ohterwise we are.
if (period)
midSentence = false;
else
midSentence = true;
}
My Checking output and my standard output is not what was expected, what did I do wrong?