2

I have assignment for class to write a code, that will count the number of appearances of a specific word in a .txt file case insensitive

int main(){
    char U[50];
    string a;
    int number=0;
    cout<<"name of file"<<endl;
    cin.getline(U,50);
    ifstream text(U,ios_base::binary);
    if(!text){
        cout<<"nonexisting"<<endl;
        return 0;
    }
    cin>>a;
    transform(a.begin(), a.end(), a.begin(), ::tolower);
    string word;
    while(text>>word){
        transform(word.begin(), word.end(), word.begin(), ::tolower);
        if(!a.compare(word))number++;       
    }
    cout<<number;
    text.close();
    return 0;
}

The problem is program counts 32 words in a file but there are 40 of them

here is my solution to the problem

int main(){
char U[50];
string a;
int number=0;
cout<<"name of file"<<endl;

cin.getline(U,50);
ifstream text(U);
if(!text){
cout<<"nonexisting"<<endl;
return 0;
}

cin>>a;

transform(a.begin(), a.end(), a.begin(), ::tolower);
string word;
while(text>>word){
    transform(word.begin(), word.end(), word.begin(), ::tolower);
    if (word.find(a) != string::npos)number++;

}   
cout<<number;
text.close();
awashima
  • 94
  • 6
  • For starters, if you want to count something, you should probably be using [`std::count` or `std::count_if`](http://en.cppreference.com/w/cpp/algorithm/count). To continue, I would suggest reading all words into a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) (which can be done in a single pass, no loop for you). Might not solve your problem, but will make the code simpler. – Some programmer dude Feb 10 '17 at 22:47
  • 1
    As for your problem, why are you opening a *text file* in *binary mode*? That could be a possible problem. – Some programmer dude Feb 10 '17 at 22:47
  • Possible duplicate of [C++, count repeated words in the string and display](http://stackoverflow.com/questions/11833882/c-count-repeated-words-in-the-string-and-display) – A user Feb 10 '17 at 22:51
  • lapsus calami my previous assignment was with binary so a mistake, and no it doesn't solve the problem but thx for constructive criticism – awashima Feb 10 '17 at 22:51
  • thank you for the link but the problem should be solvable without vector or map – awashima Feb 10 '17 at 22:54
  • `ios_base::binary` You are reading a **text** file. Why binary? There is no point using it *even if it doesn't solve your problem*. – n. m. could be an AI Feb 10 '17 at 23:14
  • In addition to previous comments, you might also want to consider what results you expect if the word is "dud", and the file contains "dududud". – Peter Feb 10 '17 at 23:15
  • 1
    Show a file it fails to count properly. – n. m. could be an AI Feb 10 '17 at 23:18
  • Have you thought about *punctuation*? The string `"hello,"` is not equal to `"hello"`. – Some programmer dude Feb 10 '17 at 23:54
  • And just because I was bored, if you wonder how a program like this would look like using only standard containers and algorithms, take a look [at this little program](https://gist.github.com/pileon/da7868295e14c0738f7a3c0536acbc35). It *will* have the same problem you're asking about, because of the punctuation issue, but it is simple and very easy to follow and understand. – Some programmer dude Feb 11 '17 at 00:06

1 Answers1

0

My guess is that the 8 words that are not counted have a newline char at the end, that is that they appear at the end of lines in the file. Can you check if that is the case? According to http://www.cplusplus.com/reference/string/string/operator%3E%3E/, the >> operator parses using whitespace as separators.

GigaRohan
  • 747
  • 2
  • 8
  • 21
  • Newline is a whitespace character. – n. m. could be an AI Feb 10 '17 at 23:17
  • @n.m. From the linked page above, "Notice that the istream extraction operations use whitespaces as separators". That makes me think that in this case only literal whitespaces are used. Anyway, evidently that's true because OP gets expected count after fixing for this. – GigaRohan Feb 10 '17 at 23:24
  • @GigaRohan No, "whitespace" doesn't mean "the space character". Also, don't link cplusplus.com, it's full of errors. http://en.cppreference.com/w/cpp/string/byte/isspace – n. m. could be an AI Feb 10 '17 at 23:26
  • @n.m Yea, you're right. I wonder then how/why things worked for awashima – GigaRohan Feb 10 '17 at 23:41
  • @GigaRohan it didn't the program counted only words that have whitespace left and right but if there is a comma or exclamation program doesn t count it and i don't know how to solve it – awashima Feb 11 '17 at 13:49