1
0

static.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // static.cpp -- using a static local variable
  2. #include <iostream>
  3. // constants
  4. const int ArSize = 10;
  5. // function prototype
  6. void strcount(const char * str);
  7. int main()
  8. {
  9. using namespace std;
  10. char input[ArSize];
  11. char next;
  12. cout << "Enter a line:\n";
  13. cin.get(input, ArSize);
  14. while (cin)
  15. {
  16. cin.get(next);
  17. while (next != '\n') // string didn't fit!
  18. cin.get(next); // dispose of remainder
  19. strcount(input);
  20. cout << "Enter next line (empty line to quit):\n";
  21. cin.get(input, ArSize);
  22. }
  23. cout << "Bye\n";
  24. // code to keep window open for MSVC++
  25. /*
  26. cin.clear();
  27. while (cin.get() != '\n')
  28. continue;
  29. cin.get();
  30. */
  31. return 0;
  32. }
  33. void strcount(const char * str)
  34. {
  35. using namespace std;
  36. static int total = 0; // static local variable
  37. int count = 0; // automatic local variable
  38. cout << "\"" << str <<"\" contains ";
  39. while (*str++) // go to end of string
  40. count++;
  41. total += count;
  42. cout << count << " characters\n";
  43. cout << total << " characters total\n";
  44. }