This lab will help you practice with function design, strings (i.e. variables/objects of the string class), and loops. (There are also options about using vectors...)
Write a program that reads a person's name in an informal notation/order (i.e. 'First Middle Last' or 'First M. Last') and then outputs their name in a more formal notation/order: 'Last, First M.'.
As an example, you might have the program interaction look something like (the parts in this color are typed by the user):
$ ./name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary Average User Your formal name would be: User, Mary A. Thanks for Formalizing with us today! Have a pompous day! $ ./name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary A. User Your formal name would be: User, Mary A. Thanks for Formalizing with us today! Have a pompous day! $ ./name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary User Your formal name would be: User, Mary Thanks for Formalizing with us today! Have a pompous day! $ ./name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: mary user Your formal name would be: User, Mary Thanks for Formalizing with us today! Have a pompous day! $ ./name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: MARY AVERAGE USER Your formal name would be: User, Mary A. Thanks for Formalizing with us today! Have a pompous day! $
Tip: It is easier to use three variables instead of one longer one that must then be broken down into parts and re-arranged. (If you desperately want to use only a single variable to hold the user's whole name, see the options below.)
Tip: There have been input support functions we've developed in lecture that you might find helpful in this program. Also some of the formatting issues are more cluttery than meaningful — not to mention repetitious — and therefore would be excellent functions.
How can you extract just the initial from the user's middle name? (Hint: Where — that is, what position — is the middle initial in the middle name?)
How can you make the user's middle name optional?! (Hint: Would peek be of use here?) (We've done optional beginnings and optional endings, why not an optional middle?)
Not that this would happen in your program, but what is wrong with the following code? (Remember that comments mention what would or should happen — they are not the problem nor is the absence of the code they describe!!! Any problems are in the actually-present code — in what it does or how it represents its own commentary.)
string s, t; string::size_type i; // a relative position for an element // fill in s (probably from the user) // make i equal to a particular character's position in s t = ' ' + s[i] + '.'; // concatenating a space before and a dot after
How might you fix it? (There may be several ways...)
How did the user's name become [properly] capitalized in the output of the last two sample runs above? They entered it in all lowercase on one and all uppercase on the other..! (Don't these people have any self-respect? Or are they just the epitome of laziness? *shrug*) (And someone should teach them some netiquette so they'll stop SHOUTING at everyone!)
This assignment is (Level 2).
Add (Level 0.5) to allow for correct capitalization of hyphenated names. (Like Rebecca Romain-Stamos.)
Add (Level 0.5) to allow for correct capitalization of apostraphized names. (Like D'Augustino or O'Brien.)
But be careful! Many company/store names will end with possessive forms like: Roman's or James'. Don't let these things bother your program and it'll be worth another (Level 1) to you!
Add (Level 1) to allow for correct capitalization of Mc and Mac names. (Like McIntyre or MacTavish.) (Don't worry about Mackey vs. MacKenzie — those kinds of issues would go into the following bit:)
You can also add (Level 1) for a [maximum] one page discussion on why accounting for De, La, and Le name prefixes would be much trickier. Suggest at least one possible implementation and then show why it won't work.
Add (Level 1.5) to make a single function to aid in the implementation of all three of the above capitalization-correction processes. (Abstraction is a powerful tool..!)
Perhaps it could be called something like:
fix_caps(user_text, "-"); fix_caps(user_text, "'"); fix_caps(user_text, "Mc"); fix_caps(user_text, "Mac");
Add (Level 2.5) to read the whole line (i.e. use getline) and then split it into names at spacing.
What if they have multiple spaces between their names? Tabs?
Would a function that replaced [all] tabs with single spaces in a string be of use? What about a function to replace [all] multi-space sequences with single spaces?
What if I told you there was a single function that did both of those tasks!?
(Hmm...where have I seen that before?)
Can there be new-lines between their names?
What if they comma-separate their names? Can you even account for things like this? (Note: I'm not asking you to — I'm asking if you could!!)
Would a function which split a string in two at a specific position be of use? What about a function to split a string in two at a specific character?
(Hmm...where have I seen that before?)
Add (Level 4) to use a vector to allow the user to have any number of name parts: Cher, Bob Jones, Ishmael Renfroe Juarez, Maria Donna Ignowsky Herberts, etc. (Note that these are not aliases of themselves, but multiple middle names.)
Note that there are at least two ways to do this. First we could make a vector of string objects:
+-----------------------+ | +===============+ | | | M | a | r | y | | | +===============+ | +-----------------------+ | +===================+ | | | Q | u | e | e | n | | | +===================+ | +-----------------------+ | +=======+ | | | o | f | | | +=======+ | +-----------------------+ | +===================+ | | | S | c | o | t | s | | | +===================+ | +-----------------------+
So each element of the vector is a string.
Or, we could just have a single long string (as in the option above) and use the vector to hold positions where we found word breaks (similar to the option above):
+---------------------------------------------------------------------------+ | M | a | r | y | | Q | u | e | e | n | | o | f | | S | c | o | t | s | +---------------------------------------------------------------------------+ +-----------------+ | 4 | 10 | 13 | +-----------------+
There may be other ways...let me know if you think of one...
Add (Level 1) to place any string processing functions in a library. Add another (Level 0.5) to append it to the strextra library you made for one of the other labs.