// // Program name: name_with_frame // Author: Jeffrey D. Oldham // Modified by: B. Massingill // // Purpose: print an uppercase name framed by characters. // // Input: a string of text representing a name. // a name // Output: the name, in upper case, framed by a box of asterisks. // #include #include // has EXIT_SUCCESS #include // has setw() #include // has toupper() // Function prototypes. (See comments below.) void obtainName(char nm[], const int nmLength); void upperCase(char name[]); void printFramedName(const char name[]); void printStarLine(const int width); void printFrameEdgeLine(const int width); void printCenteredName(const int width, const char nm[]); void printRepeatedChar(const char c, const int num); // Main program. int main() { const int nameLength = 10000; char name[nameLength]; obtainName(name, nameLength); upperCase(name); printFramedName(name); return EXIT_SUCCESS; } // Function definitions. // Pre: length of "nm" is at least "nmLength". // Post: 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; } // Pre: "name" contains a null-terminated string. // Post: "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; } // Pre: "name" contains a null-terminated string. // Post: "name" is printed, centered in a frame of asterisks. void printFramedName(const char name[]) { const int width = strlen(name) + 4; printStarLine(width); printFrameEdgeLine(width); printCenteredName(width, name); printFrameEdgeLine(width); printStarLine(width); return; } // Pre: width > 0. // Post: a line of "width" asterisks is printed. void printStarLine(const int width) { printRepeatedChar('*', width); cout << endl; return; } // Pre: width > 0. // Post: a line of length "width" is printed, consisting of an // an asterisk, width-2 blanks, and another asterisk. void printFrameEdgeLine(const int width) { printRepeatedChar('*', 1); printRepeatedChar(' ', width-2); printRepeatedChar('*', 1); cout << endl; return; } // Pre: "nm" contains a null-terminated string, and "width" = its // length = 4. // Post: a line of width "width" is printed, beginning and ending // with asterisks and with "nm" centered. void printCenteredName(const int width, const char nm[]) { printRepeatedChar('*', 1); printRepeatedChar(' ', 1); cout << nm; printRepeatedChar(' ', 1); printRepeatedChar('*', 1); cout << endl; return; } // Pre: num >= 0. // Post: "num" copies of character "c" are printed. void printRepeatedChar(const char c, const int num) { int count; for (count = 0; count < num; ++ count) cout << c; return; }