sayings1.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // sayings1.cpp -- using expanded String class
  2. // compile with string1.cpp
  3. #include <iostream>
  4. #include "string1.h"
  5. const int ArSize = 10;
  6. const int MaxLen =81;
  7. int main()
  8. {
  9. using std::cout;
  10. using std::cin;
  11. using std::endl;
  12. String name;
  13. cout <<"Hi, what's your name?\n>> ";
  14. cin >> name;
  15. cout << name << ", please enter up to " << ArSize
  16. << " short sayings <empty line to quit>:\n";
  17. String sayings[ArSize]; // array of objects
  18. char temp[MaxLen]; // temporary string storage
  19. int i;
  20. for (i = 0; i < ArSize; i++)
  21. {
  22. cout << i+1 << ": ";
  23. cin.get(temp, MaxLen);
  24. while (cin && cin.get() != '\n')
  25. continue;
  26. if (!cin || temp[0] == '\0') // empty line?
  27. break; // i not incremented
  28. else
  29. sayings[i] = temp; // overloaded assignment
  30. }
  31. int total = i; // total # of lines read
  32. if ( total > 0)
  33. {
  34. cout << "Here are your sayings:\n";
  35. for (i = 0; i < total; i++)
  36. cout << sayings[i][0] << ": " << sayings[i] << endl;
  37. int shortest = 0;
  38. int first = 0;
  39. for (i = 1; i < total; i++)
  40. {
  41. if (sayings[i].length() < sayings[shortest].length())
  42. shortest = i;
  43. if (sayings[i] < sayings[first])
  44. first = i;
  45. }
  46. cout << "Shortest saying:\n" << sayings[shortest] << endl;;
  47. cout << "First alphabetically:\n" << sayings[first] << endl;
  48. cout << "This program used "<< String::HowMany()
  49. << " String objects. Bye.\n";
  50. }
  51. else
  52. cout << "No input! Bye.\n";
  53. // keep window open
  54. /* if (!cin)
  55. cin.clear();
  56. while (cin.get() != '\n')
  57. continue; */
  58. return 0;
  59. }