Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this program is to read in several sentences. If the sentence contains a color - - that is found in the Colors.txt

The goal of this program is to read in several sentences. If the sentence contains a color--that is found in the Colors.txt file, that color is replaced with the corresponding phrase found in the Phrases.txt file.
Colors.txt contains names of colors. The names are only one word, such as red, blue, etc. Sentences.txt contain up to 10 sentences, that *may* contain at least one color in it that is found in the Colors.txt file. SwapPhrases.txt contains short phrases that will correspond to the colors in Colors.txt. There is one phrase for each color.
So there are three files to be read: Colors (7 of them), sentences ( up to 10) and phrases (7, to substitute for the colors). I will provide the three files, but you may write your own files and name them accordingly.
Your project will contain three files: Driver.cpp, which will contain the main function, and Functions.h and Functions.cpp, which will contain the functions to be called from main. Functions.h will also contain struct that holds SentenceData. That includes:
int numSwapped[SIZE]{};
string sentences[SIZE]{};
int total{0};
vector rvColors;
vector rvPhrases;
ofstream output;
string resultsOriginal;
string resultsSwapped;
The main function starts with displaying the class Header, by calling a function. Main contains the variable declarations:
SentenceData data;
int numSwapped[SIZE];
vector vColors;
string sentences[SIZE];
vector vPhrases;
plus other variables needed to keep track of items. Main also contains the default filename assignments into strings,
string colorFile {"Colors.txt"};
string sentenceFile {"Sentences.txt"};
string swapPhrasesFile {"SwapPhrases.txt"};
string outputFile {"Out.txt"}; //or whatever you like here
and all of the calls to the functions. Also, it has the output file ofstream object declared and the output file opened and checked.
Ask the user if they want to enter a filename for each file or if they want to use the default value. Dont make the user type in all of the filenames if they want to use the default. You may do this in main, or in a function (or more than one). For instance, I asked them to enter 0 if they wanted the default.
Call the Read function for each file. Pass in the vectors by reference, and pass in the array as usual. In main, check the bool return from each Read function to make sure the file was opened and read. If not, tell the user and exit the program.
Call the WriteOriginal function, which writes to the output file. It writes the original sentences, colors, phrases to the output file. It is passed the information to be written as well as the output filestream object.
In the branch where all three files were successfully read, call a function SwapColors, which returns an int, the number of sentences that were modified. There will be five arguments to this function. This is where you check all of the sentences to see if they contain any of the colors. Any individual color will only be in sentence one time, however, a sentence may have more than one color in it. A sentence may begin with a color, hence the first letter is capitalized. You will have to take that into consideration for your search. If a color is located in the sentence, that color is replaced with a corresponding phrase. If the color is the first word in the sentence, the corresponding phrase must also be capitalized so the sentence is grammatically correct. The total number of replacement actions for each sentence is stored in the NumSwapped array.
Ask for the output filename, open the output file, and call the function WriteResults. It also writes to the output file. In WriteResults, write the total number of sentences analyzed, the total that were modified, and the modified sentences. For each sentence state how many colors were swapped out. The output file must contain your name and program #.
In addition to writing the results to the file, display the results to the user in a cout.
When the program has finished executing, display a Good-bye message, by calling a function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions