-13

I wrote a program in Turbo C++ counting the number of occurrences of the string "HOTEL" in a data file. But it's always giving me the value 0. What am I doing wrong?

//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.DOCX",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();
}
curls
  • 382
  • 1
  • 3
  • 16
Abhimanyu
  • 43
  • 1
  • 7
  • 2
    Is the file `"DATA.DOCX"` in MS Word format? If so, it will be a difficult task. – MikeCAT Sep 17 '15 at 14:37
  • 8
    PLEASE DON'T SHOUT. *Edit*: I've removed the shouting for you, and other irrelevancies. Please look at the edit so you know what to do for next time. More details about what you think is wrong would also be good. – T.J. Crowder Sep 17 '15 at 14:37
  • Have you tried using a plain text file? – xxbbcc Sep 17 '15 at 14:38
  • 1
    Examine the condition in your loop closely. And use text files, not MS Word files. – molbdnilo Sep 17 '15 at 14:38
  • I wrote a program using ___CAPS LOCK ON___ – Richerd fuld Sep 17 '15 at 14:38
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Sep 17 '15 at 14:39
  • 2
    Shouldn't that be: `while ( !ifs.eof() )`? – Johnny Mopp Sep 17 '15 at 14:39
  • 6
    @JohnnyMopp [Not really](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Sep 17 '15 at 14:44
  • 1
    Well I see from your question and your code that you love caps lock... `if (strcmp(compare,"HOTEL")==0)` does your data file also shout? because if not, you might need to use `strcmpi()` to get a case insensitive version of string compare. Also, what did your debugger do when you hit a string compare that you thought should return that they are the same? – RyanP Sep 17 '15 at 14:44
  • 2
    @JohnnyMopp Yes and no, yes the condition is inverted, no you shouldn't use .eof() as a loop condition (πάντα ῥεῖ beat me to it) – Borgleader Sep 17 '15 at 14:44
  • Turbo C++ is ancient. You should work with a new compiler. And use `std::string`. And have `main` return `int`. – Christian Hackl Sep 17 '15 at 15:51
  • @MikeCAT , What's the problem with MS WORD files? – Abhimanyu Sep 17 '15 at 15:58
  • @T.J.Crowder , What's shouting? Sorry But I did not understand – Abhimanyu Sep 17 '15 at 15:58
  • @Abhimanyu: On the 'net (and most other places), ALL CAPS is shouting. Surely if you just looked at my edit, and my comment, that was fairly obvious? – T.J. Crowder Sep 17 '15 at 16:00
  • @T.J.Crowder . OK Thanks . But any help regarding the program? – Abhimanyu Sep 17 '15 at 16:01
  • @Abhimanyu: There's all sorts of help above. – T.J. Crowder Sep 17 '15 at 16:03
  • @Abhimanyu: Microsoft Word **data** files are not in simple text. The contain other information, such as fonts and attributes (bolding, italic, etc) used by each word and letter, as well as other stuff. If you want to read MS Word files, please search internet for "MSDN read word file c++". – Thomas Matthews Sep 17 '15 at 16:05
  • @RyanP , There's no shouting in my data file . I have checked it. But it still does not work. – Abhimanyu Sep 17 '15 at 16:07
  • @ThomasMatthews I changed my file name to "DATA.TXT" and now its working. Thanks. – Abhimanyu Sep 17 '15 at 16:10

1 Answers1

1

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();
}
Darrell
  • 653
  • 3
  • 17