Topical Information

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...)

Program Information

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.

Thought Provoking Questions

  1. 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?)

  2. 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?)

  3. 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...)

  4. 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).

Options