File Handling : Wrong Output

I have to create a program which counts number of time the word "he" irrespective of case appears in a text file. I have written the program,it compiles but is showing wrong output if you type "he" in a sentence,if you just type it independently (without any other word/without sentence),any number of times,it shows the correct count.Please help.
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
#include<fstream>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
    string content ;
    int len,count_of_he = 0 ;
    char ch, word[20];
    ofstream f1("Akshat.txt",ios::out);
    cout<<"\nEnter The Content for the file : " ;
    getline(cin,content);
    f1 << content ;
    f1.close();
    ifstream f2("Akshat.txt" , ios :: in);
    while (f2>>word)
    {
       len = strlen(word);
       if (!strcmp(word,"He") && (len == 2));
       count_of_he++;
    }
    f2.close();
    cout<<"\nThe number Of Time He Appears In The Sentence is = " << count_of_he ;
    return 0;
}
Last edited on
Have you checked the contents of your "Akshat.txt" file, to see if it contains what you expect it to contain?

(Hint: it probably doesn't.)
My changes to make your code work,

@10 std::string word;
@19 for (auto& ch : word) { ch = tolower(ch); };
@24 if (word == "he")
if (!strcmp(word,"He") && (len == 2));
main.cpp(20): warning C4390: ';': empty controlled statement found; is this the intent?

@MikeyBoy i am trying to make the user input the data and then count number of time he appears is you look closely. With an already existing file,I am able to count the number of time he appears. The thing is i have to create a file,write the content in it and count in the same program.

@Thomas1965 what genius compiler is that,reporting an empty control statement? I use GNU/GCC it never does this,name please? though it didn't made any difference removing the ;
It's Visual Studio 2017
You comparison is too complicated.
if (strcmp(word, "He") == 0) should do. If the word is He the length is automatically 2

Enter The Content for the file : He said He is fine.
The number Of Time He Appears In The Sentence is = 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

string WORD = "he";

string lc( string s )
{
   for ( char &c : s ) c = tolower( c );
   return s;
}

int main()
{
   ifstream in( "infile.txt" );
   cout << "Number = " << count_if( istream_iterator<string>{ in }, {}, []( string s ){ return lc( s ) == WORD;} );
}
@Thomas1965 I had confusion that what if sentence is : He has a sister,her name is abcd.
Wouldn't it count "he" in her also?

Also,If you would be kind enough please post the complete code with your edits?
Last edited on
@lastchance too complicated for me man,I have not yet got hang of <algorithm> or <iterator>,Just beginning c++.
Wouldn't it count "he" in her also?

No in the current state it will only count He.
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
#include<fstream>
#include<iostream>
#include<string.h>
#include<string>

using namespace std;

int main()
{
  const int EQUAL = 0;
  string content;
  int len, count_of_he = 0;
  char ch, word[20];
  ofstream f1("Akshat.txt", ios::out);
  cout << "\nEnter The Content for the file : ";
  getline(cin, content);
  f1 << content;
  f1.close();
  ifstream f2("Akshat.txt", ios::in);
  while (f2 >> word)
  {
    if (strcmp(word, "He") == EQUAL)
      count_of_he++;
  }
  f2.close();
  cout << "\nThe number Of Time He Appears In The Sentence is = " 
       << count_of_he;

  return 0;
}

If you want to count he or she you would need to convert word to lower case and use strstr instead of strcmp.
Try it first, if you can't get it to work I will post the full solution.
But still how come
if (strcmp(word,"He")==0)
gives wrong output
but assigning 0 to a const int doesn't? Like howw??
Last edited on
In Thomas1965's example, he simply replaced the 0 with the equivalent constant "EQUAL", which also is given the value 0. The logic is still the same as doing !strcmp(...).

1
2
const int EQUAL = 0;
strcmp(word, "He") == EQUAL

is the same as
1
2

strcmp(word, "He") == 0

is the same as
1
2

!strcmp(word, "He")


Hope that answers your question.
Last edited on
Topic archived. No new replies allowed.