-3
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
    ifstream fin;
    char ch[30];
    int count = 0;
    fin.open("story.txt");
    while (!fin.eof())
    {
        fin>>ch;
        if(ch == "the" || ch == "The")
        {
        count++;
        cout<<" "<<ch;
        }

     }

    fin.close();
    cout<<" Number of the's in the file : "<<count;

    
}

Not supposed to use strcompi() function.(or any other functions that could help compare the "ch" with "the")

The count gives zero output because the if condition doesn't work

This is my code, what could be the problem over here.**

  • 1
    This cannot be done in C++ (or C) without using functions. Note that you already violated this restriction by calling `open` for the `ifstream`. Furthermore comparing char arrays won't work, if you use `==`, since this only compares memory locations of the arrays. – fabian Jun 03 '21 at 12:15
  • Welcome! Consider being a bit more elaborate on what are you trying to achieve, what went wrong in your opinion and so forth. Also note that this style of coding is not really of C++. Consider using standard library containers instead of arrays. – rawrex Jun 03 '21 at 12:16
  • Have you tried using `std::string` instead of a char array? – The Dreams Wind Jun 03 '21 at 12:17
  • 1
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Jun 03 '21 at 12:29
  • Also, `main` is a function :-) – Jeffrey Jun 03 '21 at 12:30
  • `operator >>` and `<<` are functions. `fin.close()` is a function. And if you were to use `std::string`, using `==` would be a function call. – PaulMcKenzie Jun 03 '21 at 12:37

2 Answers2

1

You can't compare char arrays with ==, but you can with std::string.

Change

char ch[30];

to

std::string ch;
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • Yes it worked great thank you. Kindly explain your answer to me if you can. – Saad Rahman Jun 03 '21 at 17:15
  • @SaadRahman The explanation is that your program reads and inspects words. Words are called strings. And `std::string` is the standard c++ way of using strings. If you use `std::string` instead of `char[]` arrays like you had, everything about string manipulation is easier. `char[]` arrays are more like C-programming and more difficult to work with correctly, as you have seen with the incorrect attempt at comparison which led to your question. – acraig5075 Jun 04 '21 at 06:03
0

If you replace char ch[30]; with std::string ch, it looks like it would work. Maybe with some minor adjustments.

The reason why it doesn't work is that ch == "the" tests whether char ch[30], decayed as a pointer, points to the same memory location as the "the" literal. And it doesn't.

If you use a std::string, the operator== for the std::string will be called and it will do the right thing: compare the two strings.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42