// // Program name: name_with_frame // Author: Jeffrey D. Oldham // Modified by: B. Massingill // // Purpose: print an uppercase name framed by characters // // Input: // a name // Output: // the name, in upper case, framed by a box made up of "*" // characters // #include #include // has setw() #include // has toupper() // Function Prototypes // precondition: nm = array to hold the name // nmLength = maximum name length // postcondition: a null-terminated string is stored in nm void obtainName(char nm[], const int nmLength); // precondition: name has a null-terminated string // postcondition: name is converted to all uppercase letters void upperCase(char name[]); // precondition: name = null-terminated string to print in the frame's // center // postcondition: frame and centered name is printed void printFramedName(const char name[]); // precondition: width = number of stars on the line // postcondition: a line with the given number of stars is printed void printStarLine(const int width); // precondition: width = line width // postcondition: print a line of given width beginning & ending with a // star void printFrameEdgeLine(const int width); // precondition: width = line width // nm = name to center in the line // width = name's length + 4 // postcondition: print a line of given width beginning and ending with // a star and with the name in the middle void printCenteredName(const int width, const char nm[]); // precondition: c =character to print // num = number of times to print the character // postcondition: the character is printed the given number of times void printRepeatedChar(const char c, const int num); int main() { const int nameLength = 10000; char name[nameLength]; // Input obtainName(name, nameLength); // Processing upperCase(name); // Output printFramedName(name); return 0; } // precondition: nm = array to hold the name // nmLength = maximum name length // postcondition: a null-terminated string is stored in nm void obtainName(char nm[], const int nmLength) { cout << "Please enter your name: "; cin >> setw(nmLength) >> nm; // now nm contains a null-terminated string return; } // precondition: name contains a null-terminated string // postcondition: name is converted to all uppercase letters void upperCase(char name[]) { int index; for (index = 0; name[index] != '\0'; ++index) name[index] = toupper(name[index]); return; } // precondition: name = null-terminated string to print in the frame's // center // postcondition: frame and centered name is printed void printFramedName(const char name[]) { const int width = strlen(name) + 4; printStarLine(width); printFrameEdgeLine(width); printCenteredName(width, name); printFrameEdgeLine(width); printStarLine(width); return; } // precondition: width = number of stars on the line // postcondition: a line with the given number of stars is printed void printStarLine(const int width) { printRepeatedChar('*', width); cout << endl; return; } // precondition: width = line width // postcondition: print a line of given width beginning & ending with a // star void printFrameEdgeLine(const int width) { printRepeatedChar('*', 1); printRepeatedChar(' ', width-2); printRepeatedChar('*', 1); cout << endl; return; } // precondition: width = line width // nm = name to center in the line // width = name's length + 4 // postcondition: print a line of given width beginning and ending with // a star and with the name in the middle void printCenteredName(const int width, const char nm[]) { printRepeatedChar('*', 1); printRepeatedChar(' ', 1); cout << nm; printRepeatedChar(' ', 1); printRepeatedChar('*', 1); cout << endl; return; } // precondition: c = character to print // num = number of times to print the character // postcondition: the character is printed the given number of times void printRepeatedChar(const char c, const int num) { int count; for (count = 0; count < num; ++ count) cout << c; return; }