Guest User

Untitled

a guest
Jan 21st, 2018
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. // File name: WordCount.cpp;
  2. // Author: Grétar Már Margrétarson;
  3. // E-mail: gretarm11@ru.is;
  4. // Assaignment: 3;
  5. // Description: A program that counts words, characters and lines and then writes out to another file and to the screen.
  6. // Last changed: 13. október, 2011;
  7.  
  8. #include <fstream>
  9. #include <iostream>
  10. #include <cstdlib>
  11.  
  12. using namespace std;
  13.  
  14. void GetFiles(ifstream& fin, ofstream& fout);
  15. void OpenFiles(ifstream& fin, ofstream& fout);
  16. void LineCount(ifstream& fin, ofstream& fout, int& lineCount);
  17. void WordCount(ifstream& fin, ofstream& fout, int& wordCount);
  18. void CharCount(ifstream& fin, ofstream& fout, int& charCount);
  19. void DisplayResults(ostream& fout, int lineCount, int wordCount, int charCount);
  20. void CloseFiles(ifstream& fin, ofstream& fout);
  21.  
  22. int main()
  23. {
  24. int lineCount = 0, wordCount = 0, charCount = 0;
  25.  
  26. ifstream fin;
  27. ofstream fout;
  28.  
  29.  
  30. GetFiles(fin, fout);
  31. OpenFiles(fin, fout);
  32. //LineCount(fin, fout, lineCount);
  33. //WordCount(fin, fout, wordCount);
  34. CharCount(fin, fout, charCount);
  35. CloseFiles(fin, fout);
  36. DisplayResults(fout, lineCount, wordCount, charCount);
  37. DisplayResults(cout, lineCount, wordCount, charCount);
  38. CloseFiles(fin, fout);
  39.  
  40.  
  41. return 0;
  42. }
  43.  
  44. void GetFiles(ifstream& fin, ofstream& fout)
  45. {
  46. char inFile[16], outFile[16];
  47.  
  48. cout << "Enter the name of your input and then output file (max 15 characters each): ";
  49. cin >> inFile >> outFile;
  50. cout << "You have chosen the file: " << inFile << " as input.\n" << "You have chosen the file: " << outFile << " as output.";
  51. }
  52.  
  53. void OpenFiles(ifstream& fin, ofstream& fout)
  54. {
  55. char inFile[16], outFile[16];
  56.  
  57. fin.open(inFile);
  58. if (fin.fail())
  59. {
  60. cout << "Input file opening failed.";
  61. exit(1);
  62. }
  63.  
  64. fout.open(outFile);
  65. if (fout.fail())
  66. {
  67. cout << "Output file opening failed.";
  68. exit(1);
  69. }
  70. }
  71.  
  72. void LineCount(ifstream& fin, ofstream& fout, int& lineCount)
  73. {
  74.  
  75. }
  76.  
  77. void WordCount(ifstream& fin, ofstream& fout, int& wordCount)
  78. {
  79. char next;
  80.  
  81. fin.get(next);
  82. while (! fin.eof())
  83. {
  84. if (next == " " || next == "\n" || next == "\t" || next == "\r")
  85. {
  86. wordCount++;
  87. }
  88. }
  89. }
  90.  
  91. void CharCount(ifstream& fin, ofstream& fout, int& charCount)
  92. {
  93. char next;
  94.  
  95. fin.get(next);
  96. while (! fin.eof())
  97. {
  98. charCount++;
  99. }
  100. }
  101.  
  102. void DisplayResults(ostream& fout, int lineCount, int wordCount, int charCount)
  103. {
  104. fout << "The input contains: \n" << lineCount << " lines\n" << wordCount << " words \n" << charCount << " characters" << endl;
  105. }
  106.  
  107. void CloseFiles(ifstream& fin, ofstream& fout)
  108. {
  109. fin.close();
  110. fout.close();
  111.  
  112. cin.get();
  113. cin.get();
  114. }
Add Comment
Please, Sign In to add comment