Question
Write a program that finds rhyming words in a poem. Specifically, the program reads an input file, line by line, and extracts the last word
Write a program that finds rhyming words in a poem. Specifically, the program reads an input file, line by line, and extracts the last word in each line. It then compares this last word with the last word from the line after it. If it finds that the 2 words rhyme (which is simply defined as the 2 words that are being compared share the same 2 last letters), then it prints out these two words to standard output (i.e. the display). But FIRST, the words must be cleaned up of any non-alphabetical characters (usually this means puncuation, since many poems have their last lines often end with a comma, a semicolon, a question-mark, etc) So if the last word on a line has punctuation marks on it, e.g. hello?!, it becomes hello. Or if the last word is dont, it becomes dont.
Finally, the program has to state how many rhyming pairs it found, or if it did not find any at all.
The program has to ask the user for the file name and has to check to see if the file exists (use the string Enter file name: for the question). If it does not exist, the program must output (via cerr) an error message: Input file opening failed. and then exit with code 1.
Your program should define at least one function. If your program does NOT have at least one function, you will not get credit for this part of the assignment, even if your program passes submit.cs grading. And remember that you can ONLY use programming techniques and instructions that we have covered in class so far.
As an example, assume there is a text file called MyPoem.txt, which contains this:
"I cannot go to school today," Said little Peggy Ann McKay. "I cannot go to school and play! I have the measles and the mumps, A gash, a rash and purple bumps. My mouth is wet, my throat is dry, Im going blind in my right eye. My tonsils are as big as rocks, Ive counted sixteen chicken pox"
(slightly modified from the poem Sick by Shel Silverstein)
The program would run as follows:
Enter file name: MyPoem.txt today and McKay McKay and play mumps and bumps There are 3 pairs of rhyming words.
Note that dry and eye were not shown, nor were rocks and pox. This is because they do not meet the programs criteria of a rhyme. Also note the last line says how many pairs of rhyming words there are. Please make the word pair singular if only 1 pair of rhyming words is found and use the plural pairs if you found more than 1 rhyming pairs.
Consider another example file called No.txt:
No means no, it always means no If I say it once it means A thousand times no!
(slightly modified from the song No! by They Might Be Giants)
The program would run as follows:
Enter file name: No.txt No rhymes found.
Note that no rhymes were found because the program only looks at adjacent lines.
You can assume that the text file containing the poem has at least 2 words per line, that the rhyme comparison is case-sensitive, and that the last words in the line contain at least 2 characters.
You will need to use multiple member functions to manipulate strings in this program. In addition, you will have to use the getline() function in the library in order to read an entire line by ifstream.
MAJOR REQUIREMENT: At least ONE function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started