Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. ------------------------------
  2. // ************************************************** **************
  3. //
  4. // count.cc
  5. //
  6. // Introduction: Count lines and words
  7. // (1) in a keyboard input, or
  8. // (2) in a file
  9. //
  10. // Uses:
  11. // (1) ./count
  12. // (2) ./count < input.txt
  13. //
  14. // ************************************************** **************
  15.  
  16. #include <cstdlib>
  17. #include <iostream>
  18. #include <string>
  19. #include <sstream>
  20.  
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. string temp = "";
  26. string word = "";
  27. int countWords = 0;
  28. string line = "";
  29. int countLines = 0;
  30.  
  31. cout << "This program counts lines and words. " << endl;
  32. cout << "Please give input. Use Ctrl + C to stop." << endl;
  33.  
  34. while ( cin )
  35. {
  36. getline( cin, line ); // read line
  37. ++countLines; // count line
  38.  
  39. std::istringstream linestream( line );
  40. while ( !linestream.eof() )
  41. {
  42. linestream >> word; // read word
  43. temp = word;
  44. ++countWords; // count word
  45.  
  46. }
  47.  
  48. cout << "Words: " << countWords << endl;
  49. cout << "Lines: " << countLines << endl;
  50. }
  51.  
  52. return EXIT_SUCCESS;
  53. }
Add Comment
Please, Sign In to add comment