// This program demonstrates how to read a string into // a string class object. // String member functions: // empty // length (or size) // clear // // Reading in strings with spaces -- use getline // cin.ignore #include #include using namespace std; int main() { string str, name; // initialize a string object string str2 = "Good Bye!"; if (name.empty()) cout << "The string is empty: " << name << endl; else cout << "here is name: " << name << endl; str = "Hello "; cout << "What is your name? "; cin >> name; cout << str << name << '!' << endl; cout << "the length of your name is: " << name.length() << '.' << endl; /* Strings with spaces // getline(inputstream, stringobject) // getline(inputstream, stringobject, char) where char is the 3rd optional parameter, and it is a delimeter -- read up until the delimeter NOT including the delimeter. */ cin.ignore(); // ignores next char in input stream cout << "now what is your name? "; getline(cin, name, '\n' ); cout << str << name << '!' << endl; cout << "the length of your name is: " << name.length() << '.' << endl; name.clear(); if (name.empty()) cout << "The string was cleared and it is empty. " << name << endl; else cout << "here is name after clear: " << name << endl; cout << str2 << endl; return 0; }