/* teststring1.cpp CIS 150 David Klick 6/30/05 Demo of some simple C-style string functions. */ #include #include using std::cout; using std::cin; int main(void) { char food[50]; // storage for favorite name char name[50]; // storage for whole name char first[20]; // storage for first name char middle[15]; // storage for middle name char last[20]; // storage for last name cout << "Please enter your favorite food: "; // safest way to get text input // limits input to 49 characters // allows blanks within input (unlike normal cin) cin.getline(food, 50, '\n'); cout << "Your favorite food is " << food << ".\n"; // get user's first middle and last names cout << "Please enter your first name: "; cin.getline(first, 20, '\n'); cout << "Please enter your middle name: "; cin.getline(middle, 15, '\n'); cout << "Please enter your last name: "; cin.getline(last, 20, '\n'); cout << "Greetings, " << first << "!\n"; cout << "Your official name is: " << last << ", " << first << ' ' << middle << '\n'; // start constructing full name using first name strcpy(name, first); // add middle name is there is one if (strlen(middle) > 0) { // add space if needed if (strlen(name) > 0) strcat(name, " "); // add middle name strcat(name, middle); } // add last name is there is one if (strlen(last) > 0) { // add space if needed if (strlen(name) > 0) strcat(name, " "); // add last name strcat(name, last); } cout << "Good-bye, " << name << ".\n"; return 0; } /* Sample output: Please enter your favorite food: Hot Tamales Your favorite food is hot tamales. Please enter your first name: David Please enter your middle name: G. Please enter your last name: Klick Greetings, David! Your official name is: Klick, David G. Good-bye, David G. Klick. */