My word count adds other files word count together...

Hi, I was wondering if anyone could help me fix my word count accumulating other files word counts and make it so where it restarts all over again and just reads the next file i enter words and not add up the past inputs words as well.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char letter; //Used to store a single character
    ifstream inFile; //Incoming file stream variable
    string fileName, word, line;
    int wordCount = 0;

    //Opening Message for the user and prompting user to enter data file
    cout << "                    *** A SIMPLE FILE PROCESSING PROGRAM ***\n" << endl;
    cout << "Enter a filename or type quit to exit: ";
    cin >> fileName;
    cout << fileName << " Data" << endl;
    cout << "***********************" << endl;
    inFile.open(fileName.c_str()); //Command to find and open a file as listed
do{

    //Read data as a line of text and store in a string
    getline(inFile, line);

    //File open validation routine
    while(!inFile)
    {
        inFile.clear();
        inFile.ignore();

        cout << "Error, File note found. Enter the correct filename: ";
        cin >> fileName;
        cout << fileName << " Data" << endl;
        cout << "***********************" << endl;
        inFile.open(fileName.c_str()); //Runtime input
    }

    while(inFile)
    {
    cout << line << endl;
    getline(inFile, line); //Read character for the next line of text to store
    }
    cout << "***********************" << endl;

    inFile.close();
    inFile.open(fileName.c_str());
    inFile >> word;

    while(inFile)
    {
        wordCount++;
        inFile >> word;
    }
    cout << fileName << " has exactly " << wordCount << " words.\n" << endl;

    inFile.close();
    cout << "Enter a filename or type quit to exit: ";
    cin >> fileName;
    inFile.open(fileName.c_str()); //Command to find and open a file as listed

}while(fileName != "quit");
    cout << "Now exiting the program........" << endl;
    return 0;
}

Wouldn't you think it would be a bunch simpler to copy the entire file into a string using the inFile.read trick, and use the string.find function?

If the memory usage worries you, you can also rewrite your function to use the logic of std::search (the same logic string.find uses), replace the first 2 iterators for the file stream, and call .get() to read byte by byte.

the code for std::search is quite simple:
http://www.cplusplus.com/reference/algorithm/search/

Also you aren't setting wordCount to zero.
I thought I set the word count to zero...
Also I am a beginner so i have not learned the trick yet
It really isn't much of a trick, you can copy a file into a string any way you want. This is something your gut should tell you "this is a wheel, I don't need to / shouldn't write this!".

but if you are curious, what I am talking about is resize() a string to be the size of the file, then using a C-Style fstream.read() to write into the string's .data()

an example is here, just replace the manual memory management with a C++ string.
http://www.cplusplus.com/reference/istream/istream/read/

But if you don't like it, like I said, you can absolutely translate the std::search algorithm to work with streams, and if you have a problem with your code we can help you.
> Also I am a beginner
┬┐why are you posting in `General' then?

> I thought I set the word count to zero...
sure you do, just at the start of your program.
if you bother yourself with pseudocode, diagram flow or a line by line execution you'll realize why that's not good enough.

here's a simplified version of your issue
1
2
3
4
5
6
7
8
9
wordCount = 0
while(cin >> filename){
	inFile.open(filename);

	while(inFile >> word)
		wordCount++;

	cout << wordCount << '\n';
}

Last edited on
Wouldn't you think it would be a bunch simpler to copy the entire file into a string using the inFile.read trick, and use the string.find function?
Reading the whole file into a string is a really bad idea because of the memory usage that bring up below.

If the memory usage worries you, you can also rewrite your function to use the logic of std::search (the same logic string.find uses), replace the first 2 iterators for the file stream, and call .get() to read byte by byte.
But why do that when cin >> string will do all the work for you?
Topic archived. No new replies allowed.