[Solved] Turbo C++ program that’s counting occurrences of a string in data file is always (incorrectly) returning zero.


As asker has said, the issue was that the file name was “DATA.DOCX” and needed to be changed to “DATA.TXT”.

//PROGRAM TO COUNT NO OF OCCURENCES OF A STRING IN A DATA FILE
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
ifstream ifs;
ifs.open("DATA.TXT",ios::in|ios::nocreate);
if (!ifs)
 { 
    cout<<"SORRY! FILE DOES NOT EXIST";
 }   
else
 {
int count=0;
char compare[20];
while (ifs.eof())
 {
    ifs>>compare;
    if (strcmp(compare,"HOTEL")==0)
     count++;
 }
ifs.close();
cout<<"NO OF OCCURENCE OF STRING 'HOTEL' = "<<count;
  }
getch();
}

solved Turbo C++ program that’s counting occurrences of a string in data file is always (incorrectly) returning zero.