Advertisement
MutahirA_

Untitled

Nov 13th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1.  
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. int main()
  8. {
  9.  
  10. char file_name[16];
  11. std::ifstream in_stream;
  12. int count = 0;
  13. char ch;
  14. bool lastWasWhite = true;
  15. std::string next;
  16.  
  17. // Ask the user for the file name
  18. std::cout << "What is the name of the file? ";
  19. std::cin >> file_name;
  20. in_stream.open(file_name);
  21.  
  22. // See if we can open the file
  23. if (!in_stream.is_open()) {
  24. std::cout << "Something went wrong when opening your file.";
  25. exit(1);
  26. }
  27.  
  28. // Read the file, one character at a time
  29. while (in_stream >> next) {
  30. do {
  31. std::cin.get(ch);
  32. }
  33. while (ch != ' ' && ch != '\n' && ch != '\t');
  34. ++count;
  35. }
  36.  
  37. /* program works mostly properly. It only completes if you use CTRL+C to end the terminal. I cannot find a way to
  38. quit the program before that, so I think it loops forever. */
  39.  
  40. in_stream.close();
  41.  
  42. std::cout << "The file " << file_name << " contains " << count << " words." << std::endl;
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement