I l@ve RuBoard |
![]() ![]() |
Exercise 1.5Write a program to ask the user his or her name. Read the response. Confirm that the input is at least two characters in length. If the name seems valid, respond to the user. Provide two implementations: one using a C-style character string, and the other using a string class object. The two primary differences between a string class object and a C-style character string are that (1) the string class object grows dynamically to accommodate its character string, whereas the C-style character string must be given a fixed size that is (hopefully) large enough to contain the assigned string, and (2) the C-style character string does not know its size. To determine the size of the C-style character string, we must iterate across its elements, counting each one up to but not including the terminating null. The strlen() standard library routine provides this service for us: int strlen( const char* ); To use strlen(), we must include the cstring header file. However, before we get to that, let's look at the string class implementation. Particularly for beginners, I recommend that the string class be used in favor of the C-style character string. #include <iostream> #include <string> using namespace std; int main() { string user_name; cout << "Please enter your name: "; cin >> user_name; switch ( user_name.size() ){ case 0: cout << "Ah, the user with no name. " << "Well, ok, hi, user with no name\n"; break; case 1: cout << "A 1-character name? Hmm, have you read Kafka?: " << "hello, " << user_name << endl; break; default: // any string longer than 1 character cout << "Hello, " << user_name << " -- happy to make your acquaintance!\n"; break; } return 0; } The C-style character string implementation differs in two ways. First, we must decide on a fixed size to declare user_name; I've arbitrarily chosen 128, which seems more than adequate. Second, we use the the standard library strlen() function to discover the size of user_name. The cstring header file holds the declaration of strlen(). If the user enters a string longer than 127 characters, there will be no room for the terminating null character. To prevent that, I use the setw() iostream manipulator to guarantee that we do not read in more than 127 characters. To use the setw() manipulator, we must include the iomanip header file. #include <iostream> #include <iomanip> #include <cstring> using namespace std; int main() { // must allocate a fixed size const int nm_size = 128; char user_name[ nm_size ]; cout << "Please enter your name: "; cin >> setw( nm_size ) >> user_name; switch ( strlen( user_name )) { // same case labels for 0, 1 case 127: // maybe string was truncated by setw() cout << "That is a very big name, indeed -- " << "we may have needed to shorten it!\n" << "In any case,\n"; // no break -- we fall through ... default: // the 127 case drops through to here -- no break cout << "Hello, " << user_name << " -- happy to make your acquaintance!\n"; break; } return 0; } |
I l@ve RuBoard |
![]() ![]() |