Question: Create a file called Anagram.cpp, which reads in a series of words from a file. For each word read in from the file, the user
Create a file called Anagram.cpp, which reads in a series of words from a file.
For each word read in from the file, the user will have the option to swap letters within that word.
You are required to use your templated doubly-linked list class to receive credit for this part of the assignment.
Therefore, your word should be stored as a List of chars.
After the user has rearranged the word, it should be written to a file, including the changes the user has made.
Specifications
The program should welcome the user with the message Welcome to the Anagram Arranger!
It should then prompt the user to enter the name of a file.
Provided the user enters a correct file name (see section regarding error checking below), then the program will read in all of the words in the file, one-by-one.
Each word should be stored in a List of chars.
It will display the word, along with its corresponding number, with the message: Word #
See below for examples.
The user will then be prompted to enter the position of two different letters in the word that the user wants to be swapped.
The program will verify the user choice by re-printing the word with carrots beneath the selected letters.
The user will then be required to confirm his or her choice with the message: Enter the position numbers of the two letters you wish to swap:
The program should accept four different input options from the user -- y, Y, yes, and Yes -- to indicate consent.
If the user indicates "no", the word will be reprinted with a prompt to enter the position of two different letters.
If the user enters yes, then the program should swap the two selected letters, and then prompt the user to indicate whether he or she would like to swap additional letters in the word.
The program should accept four different input options from the user -- y, Y, yes, and Yes -- to indicate consent.
The user should be able to continue swapping letters indefinitely.
Once the user no longer wishes to swap, the current version of the word should be written to a file named output.txt
Please see below for an incomplete example run:
Welcome to the Anagram Arranger! Please enter the name of your input file: spooky.txt Word #1 is zombie 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 1 2 z o m b i e ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: o z m b i e Want to keep rearranging? (y/n): y 1: o 2: z 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 3 5 o z m b i e ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: o z i b m e Want to keep rearranging? (y/n): n Word #2 is mummy 1: m 2: u 3: m 4: m 5: y Enter the position numbers of the two letters you wish to swap: etc...
A complete Example:
This example assumes an input file named words.txt with the following content:
Spring Summer Fall Winter
Output of running Anagram.cpp
Welcome to the Anagram Arranger! Please enter the name of your input file: abc.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: 123.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: words.txt Word #1 is Spring 1: S 2: p 3: r 4: i 5: n 6: g Enter the position numbers of the two letters you wish to swap: 3 5 S p r i n g ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: S p n i r g Want to keep rearranging? (y/n): yes 1: S 2: p 3: n 4: i 5: r 6: g Enter the position numbers of the two letters you wish to swap: 1 6 S p n i r g ^ ^ Are these the letters you wish to swap? (y/n): yes The new word is: g p n i r S Want to keep rearranging? (y/n): n Word #2 is Summer 1: S 2: u 3: m 4: m 5: e 6: r Enter the position numbers of the two letters you wish to swap: 2 3 S u m m e r ^ ^ Are these the letters you wish to swap? (y/n): Yes The new word is: S m u m e r Want to keep rearranging? (y/n): Yes 1: S 2: m 3: u 4: m 5: e 6: r Enter the position numbers of the two letters you wish to swap: 4 5 S m u m e r ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: S m u e m r Want to keep rearranging? (y/n): n Word #3 is Fall 1: F 2: a 3: l 4: l Enter the position numbers of the two letters you wish to swap: 1 2 F a l l ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: a F l l Want to keep rearranging? (y/n): n Word #4 is Winter 1: W 2: i 3: n 4: t 5: e 6: r Enter the position numbers of the two letters you wish to swap: 1 3 W i n t e r ^ ^ Are these the letters you wish to swap? (y/n): n 1: W 2: i 3: n 4: t 5: e 6: r Enter the position numbers of the two letters you wish to swap: 1 4 W i n t e r ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: t i n W e r Want to keep rearranging? (y/n): y 1: t 2: i 3: n 4: W 5: e 6: r Enter the position numbers of the two letters you wish to swap: 2 5 t i n W e r ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: t e n W i r Want to keep rearranging? (y/n): y 1: t 2: e 3: n 4: W 5: i 6: r Enter the position numbers of the two letters you wish to swap: 3 4 t e n W i r ^ ^ Are these the letters you wish to swap? (y/n): y The new word is: t e W n i r Want to keep rearranging? (y/n): n Bye!
Corresponding output.txt for above example: g p n i r S S m u e m r a F l l t e n W i r
Required Error Checking
Your Anagram.cpp should also do error checking for the following cases only:
The user inputs a position number that is higher than the position of the last character in the word
The user reverses the order of the two positions by giving the higher number followed by the lower number
The user enters the same position number twice.
For the above errors, the program should print the message Invalid entry! and allow the user to try again, as shown in the example below.
Additionally, the program should correctly handle the below error:
The user enters an incorrect name for a file.
Error checking should be completed using a loop to handle multiple invalid inputs
Error message must be:
Sorry. I cannot find a file by that name! Please enter the name of a valid input file:
Please see below for examples of how to handle invalid input.
Error Checking Example: Welcome to the Anagram Arranger! Please enter the name of your input file: ddd.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: nnn.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: splooky.txt Sorry. I cannot find a file by that name! Please enter the name of a valid input file: spooky.txt Word #1 is zombie 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 4 2 Invalid entry! 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 1 10 Invalid entry! 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 2 2 Invalid entry! 1: z 2: o 3: m 4: b 5: i 6: e Enter the position numbers of the two letters you wish to swap: 2 3 z o m b i e ^ ^ Are these the letters you wish to swap? (y/n): yes The new word is: z m o b i e Want to keep rearranging? (y/n): etc.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
