Here is the problem from my Programming class:
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.

I have figured out how to get all lower case letters in the file but cannot figure out how to make the first letter in every sentence uppercase. Thanks for any help you can give! Sorry if I didn't post this correctly, this is my first time.

#include <iostream>
#include <fstream>
#include <cctype>	// Needed for tolower function.
using namespace std;

int main()
{
	const int SIZE = 81;		// Constant size of filenames

	// Arrays for file names
	char fileName1[SIZE];
	char fileName2[SIZE];

	char ch;		// Holds character 

	
	ifstream inFile;	// Input file stream object
	fstream outFile;	// Output file stream object
   
  
   // Get FIRST file from user
   cout << "Please enter the first file (sentences.txt).\n";
   cin >> fileName1;

   // Open file for input
   inFile.open (fileName1);

   // Test file for errors.
   if (!inFile)
   {
      cout << "The file " << fileName1 
           << " could not be opened.";
      exit(0);
   }

   // Get SECOND file(user created) from user 
   cout << "Please enter a new file name to save the conversion(ie: [filename].txt).\n";
   cin >> fileName2;

   // Open and create fileName2 for output
   outFile.open (fileName2, ios::out);

   // Process files
   inFile.get(ch);
   while(!inFile.eof())
   {
	   outFile.put(tolower(ch));
	   inFile.get(ch);
   }

   // Close files
   inFile.close();
   outFile.close();
   
   // Let user know conversion is finished
   cout << "File conversion is complete.\n";
	return 0;
}

Recommended Answers

All 8 Replies

Other than minor formatting and C++ in the title (what forum is this?), your first post is quite good.

What constitutes the beginning of a sentence? How can your program recognize it?

Other than minor formatting and C++ in the title (what forum is this?), your first post is quite good.

What constitutes the beginning of a sentence? How can your program recognize it?

I tried using the seekp member function but could not figure out where it needed to go. It just made my output file all over the place.

This is what I tried to get the first letter in the sentence:

inFile.seekp(0L, ios::beg)

I'm just not sure where to put it. I had a lot of problems with loops in my past programing class.

I tried using the seekp member function but could not figure out where it needed to go. It just made my output file all over the place.

This is what I tried to get the first letter in the sentence:

inFile.seekp(0L, ios::beg)

I'm just not sure where to put it. I had a lot of problems with loops in my past programing class.

Why? You don't need seekp() at all.

Try answering my questions. I asked them for a reason.

The specification says that the sentences are separated by periods. I'm assuming there won't be a space after the period. Keep track of the last character you read, if it's a period, capitalize the next letter. If there is a space, you'll need to skip over it before capitalizing.

Why? You don't need seekp() at all.

Try answering my questions. I asked them for a reason.

Sorry Walt, I guess I don't really understand what you are asking.

The specification says that the sentences are separated by periods. I'm assuming there won't be a space after the period. Keep track of the last character you read, if it's a period, capitalize the next letter. If there is a space, you'll need to skip over it before capitalizing.

the text file from the book to use for testing is this:

this is a TEST file.
the sentences in this file are of varying
lengths. some span lines.

others DO NOT.

Okay, so trace through that sample file with a pencil and paper. Where are the capital letters necessary? What separates the sentences from each other?

Once I hit the period, there is a ________ or a _________. After that, if there is a letter, capitalize it.

Now translate that into code.

Sorry Walt, I guess I don't really understand what you are asking.

Really... Then we need to teach a basic English concept.
Q1: What constitutes the beginning of a sentence? Rephrase: What is the definition of a sentence? How do you know when a sentence ends? How can you tell what the first character of a sentence is?
If you don't understand these questions, you can't really program the task.

the text file from the book to use for testing is this:

this is a TEST file.
the sentences in this file are of varying
lengths. some span lines.

others DO NOT.

How many sentences are there? What is the first letter of each sentence? How can you tell?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.