简体   繁体   中英

Comparing Strings (C++)

I am trying to get the amount of occurrences in a file of each word in a separate file. The following code should produce the number of occurrences of each word in sl using the words in fl but instead, it outputs 0 for each words even when there are many instances. I'm trying to figure out what the problem is and could do with some help. The getNextWord() function simply returns the next word in the file.

while(fl.isWord()){
        int total = 0;
        string temp1= fl.getNextWord();
        while(sl.isWord()){
            string temp2 = sl.getNextWord();
            if(temp1.compare(temp2)!=0) total ++;
          //if(temp1 == temp2) total++;

        }
        cout << temp1 << " " << total << endl;
}

The function isWords() is in a separate class:

bool ReadWords::isWord(){
    if(file.good())
        return !eoffound;
    else
        return eoffound;

Exemplar lists:

fl content kfc cows

sl content: damn cows cows chicken apples apples kfc food fat cows

output:

kfc 0

cows 0

output should be:

kfc 1

cows 3

edited part:

string ReadWords::getNextWord(){
    file >> theWord;
    return theWord;
}

Assuming your functions for parsing the files are correct, there is a problem in the logic of the implementation. The problem is that after you get the first word from fl, you search the whole file sl, this one reaches its eof, then later searches will not work because sl is at eof.

What you need is a way to seek sl to its beginning after you finish with each word from fl.

while(fl.isWord()){
    /* seek sl to beginning; then do the rest */
    int total = 0;
    string temp1= fl.getNextWord();
    while(sl.isWord()){
        string temp2 = sl.getNextWord();
        if(temp1 == temp2) total++;
    }
    cout << temp1 << " " << total << endl;
}

EDIT: if you cannot find a way to seek the file, load all of its words into memory using vector<string> , in one pass, then do the search on this vector for each word from fl.

This will correct the logic of your implementation. HOWEVER, this is NOT to say that it is the recommended and "best" implementation (to avoid purists shouting at me :P ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM