0

So this is what I have tried doing. This is also the logic I mostly use to do word searching codes. I am basically seeing if I can find the character "g" and then I look if there are letters matching with good. If they are matching then I do count++ to count it and in the end I output my results:-



#include<iostream> //headers
#include<fstream>

using namespace std;

int main()
{
    
    char arr[10000];
    //declaration
    fstream fileis;
    
    fileis.open("fileis.txt",ios::in);
    
    while (!fileis.eof( ))      
     {     //reading loop
             
          fileis>>arr;
          cout<<arr<<endl;  
     }
     
    int count=0;
    
     for(int i=0; arr[i] != '\0'; i++)  //main loop
     {
        if(arr[i] == 'g' && arr[i+1] == 'o' && arr[i+2] == 'o' && arr[i+3] == 'd') //condition to check if the word is there
        {
                count++;     
        }
    }
        
    fileis.close();
    
    cout<<"The word good appeared "<<count<<" times."<<endl;
    
    return 0;
    
}
Shahzaib
  • 1
  • 1
  • 2
    a) your condition assumes that you have enough letters left (what if g is the last letter in the file? pretty sure you'll be indexing incorrect data (when checking for o-o-d) b) [while (!eof) is bad](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Borgleader May 31 '22 at 15:32
  • Your question would be of higher quality of you provided a [mre] of the problem, which includes the exact input required to reproduce the problem. Also, "it doesn't work" is not a helpful description of the problem. It would be more helpful if you specified the actual output and the desired output when running your program with the input you provided. – Andreas Wenzel May 31 '22 at 15:42
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default-initializing them and immediately overwriting the default values. In this case that means changing `fstream fileis; fileis.open("fileis.txt",ios::in);` to `fstream fileis("fileis.txt",ios::in);`. Also, since the file is being opened for **input**, use an input stream: `ifstream fileis; fileis.open("fileis.txt");`. And you don't need to call `fileis.close();`. The destructor will do that. – Pete Becker May 31 '22 at 15:42
  • 1
    After your read loop, `arr` contains the last word in the file. – molbdnilo May 31 '22 at 15:42
  • Are looking for literally the word "good" or for those letters consecutively, possibly as part of a word? That is, does "goodness" count or not? – molbdnilo May 31 '22 at 15:44
  • And on top of all the other problems with the shown code, `while (!fileis.eof( ))` [is always a bug](https://stackoverflow.com/questions/5605125/). In which C++ textbook did you see an example of this kind of logic? – Sam Varshavchik May 31 '22 at 16:41
  • There is no need for an array: `std::string word; while (fileis >> word) { std::transform(word.begin(), word.end(), word.begin(), tolower); if (word == "good") ++count; }` You can perform the comparison and the counting inside the reading loop. – Thomas Matthews May 31 '22 at 17:10

1 Answers1

0

Here's my suggestion:

std::string word;
count = 0;
//...
while (fileis >> word)
{
  // Convert word to all lower case for easy comparison.
  std::transform(word.begin(), word.end(), word.begin(), tolower);

  if (word == "good") ++count;
}

The above code fragment has some issues for the OP to find (such as what happens when a punctuation character is read in after "good").

By default reading a string from a stream reads in a "word", whitespace separated.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154