Can't find the bug in the code.
I am trying to find the words good
or great
in a sentence entered by a user.
It works fine when searching for good but does not work for great.
#include<iostream>
#include<fstream>
using namespace std;
bool read1();
void write()
{
ofstream fout("sentData.txt",ios::out);
char sentence[1000]={'\0'};
cout<<"Enter a sentence to check whether it contains \"good\" or \"great\" : "<<endl;
cin.getline(sentence,1000);
fout<<sentence;
}
int read()
{
ifstream fin("sentData.txt", ios::in);
char sentence[100] = { '\0' };
char good[5] = { 'g', 'o', 'o', 'd', '\0' };
int count;
bool check=0;
while (!fin.eof())
{
fin.getline(sentence, 150);
for (int i = 0; sentence[i] != '\0'; i++)
{
int k = 0;
count = 0;
for (int j = i; sentence[j] != 32; j++)
{
if (sentence[j] == good[k])
{count++;
}
k++;
}
if(count==4)
{
check=1;
break;
}
}
if(count==4)
{
break;
}
}
fin.close();
bool x=read1();
if(check==1 || x==1)
cout<<"Positive sentence."<<endl;
}
bool read1()
{
ifstream in("sentData.txt", ios::in);
char sentence[100] = { '\0' };
char great[6]={'g','r','e','a','t','\0'};
int count;
while(!in.eof())
{
in.getline(sentence,100);
for (int i = 0; sentence[i] != '\0'; i++)
{
int k = 0;
count=0;
for (int j = i; sentence[j] != 32; j++)
{
if (sentence[j] == great[k])
{count++;
}
k++;
}
if(count==5)
{
return 1;
break;
}
}
}
}
int main()
{
write();
read();
return 0;
}