-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHands_Lab7_Prob1.cpp
More file actions
36 lines (35 loc) · 1.01 KB
/
Copy pathHands_Lab7_Prob1.cpp
File metadata and controls
36 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
void countDoc(const string &filename, int &wordCount, int &charCount)
{
ifstream fin; // labeling
string word; // labeling
fin.open(filename); // open file
if (fin.is_open())
{
while (fin >> word)
{
wordCount++; // counts word
charCount = charCount + word.size(); // word.size() is the size of word = amount of characters a word has
}
}
else
{
cout << "Failed to open file" << endl;
exit(-1);
}
fin.close();
}
int main()
{
int wordCount = 0;
int charCount = 0;
string filename = "dat_hw7_prob1.txt"; // labeling & assigning file
countDoc(filename, wordCount, charCount);
cout << "Number of words in the File: " << wordCount << endl;
cout << "Number of characters, exculding spaces, in the File: " << charCount << endl;
return 0;
}