strtype4.cpp 942 B

12345678910111213141516171819202122232425262728
  1. // strtype4.cpp -- line input
  2. #include <iostream>
  3. #include <string> // make string class available
  4. #include <cstring> // C-style string library
  5. int main()
  6. {
  7. using namespace std;
  8. char charr[20];
  9. string str;
  10. cout << "Length of string in charr before input: "
  11. << strlen(charr) << endl;
  12. cout << "Length of string in str before input: "
  13. << str.size() << endl;
  14. cout << "Enter a line of text:\n";
  15. cin.getline(charr, 20); // indicate maximum length
  16. cout << "You entered: " << charr << endl;
  17. cout << "Enter another line of text:\n";
  18. getline(cin, str); // cin now an argument; no length specifier
  19. cout << "You entered: " << str << endl;
  20. cout << "Length of string in charr after input: "
  21. << strlen(charr) << endl;
  22. cout << "Length of string in str after input: "
  23. << str.size() << endl;
  24. // cin.get();
  25. return 0;
  26. }