0

I made the following program. There is a mistake in bold part. The value of count in the output I'm getting is zero. There were no errors when I compiled the code.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
    clrscr();
    void count();
    fstream file("STORY.TXT",ios::in|ios::out);
    file<<"He is playing in the ground. She\nis playinbg with her dolls.\n";
    file.close();
    count();
    getch();
}
void count()
{
    ifstream file("STORY.TXT");
    file.seekg(0);int count=0;
    while(!file.eof())
    {
        char line[10];
        **file.get(line,10,' ');
        cout<<line<<"\n";
        if(line=="HE")
            ++count;**
    }
    cout<<count;
    file.close();
}
tshepang
  • 10,772
  • 21
  • 84
  • 127
Smatik
  • 387
  • 3
  • 14
  • Your `eof()` while loop could be wrong, see [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – juanchopanza Mar 13 '13 at 08:00

1 Answers1

3

String comparison is not done through ==. That merely compares the address Replace

if(line=="HE")

with

if(!strcmp(line, "HE"))

EDIT

For case insensitive

if(!strcmpi(line, "HE"))
uba
  • 1,925
  • 14
  • 18