String Search In C++
-
Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.
void search()
{char str2[100];
char str[100];
cout<<"write some text to search:";
gets(str);
ifstream i;
i.open("stud.txt");
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
}
getch();
}
-
Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.
void search()
{char str2[100];
char str[100];
cout<<"write some text to search:";
gets(str);
ifstream i;
i.open("stud.txt");
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
}
getch();
}
Edit your post and add what (the code) you have tried.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Edit your post and add what (the code) you have tried.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Thanks For Your help in the previous question bro but it didn't worked. I already put the comparison into the while loop as you said but it's still searching for the last word. I am stuck at this point bro i don't know what to do. I also have to move to other parts of the program and complete it but i am stuck at this major point.
-
Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.
void search()
{char str2[100];
char str[100];
cout<<"write some text to search:";
gets(str);
ifstream i;
i.open("stud.txt");
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
}
getch();
}
I suspect that the problem is that you are using
i>>str2;
wherestr2
is defined as a character array. If you add a print command after that line you can see exactly what is returned by the>>
operator. You should use astring
type to get each word. Alternatively use ordinary C library functions to read each line and tokenise into words. -
I suspect that the problem is that you are using
i>>str2;
wherestr2
is defined as a character array. If you add a print command after that line you can see exactly what is returned by the>>
operator. You should use astring
type to get each word. Alternatively use ordinary C library functions to read each line and tokenise into words.Thanks for taking time to reply to me bro. Now, I would if i could what you told me but the problem is that i HAVE to make this project in Turbo C++ only and i know that this is ancient and i will go to hell for using it but plz try to help me with this project with the turbo Compiler
-
Thanks For Your help in the previous question bro but it didn't worked. I already put the comparison into the while loop as you said but it's still searching for the last word. I am stuck at this point bro i don't know what to do. I also have to move to other parts of the program and complete it but i am stuck at this major point.
How about having a boolean variable, something like..
bool FoundIt = false; //declare and initialize
.
.
.
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) //match? { FoundIt = true; // yes, set our new boolean variable break; // get out of while loop }
}
if (FoundIt)
cout<<"found ! \n";
else
cout<<"not found ! \n";"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Thanks for taking time to reply to me bro. Now, I would if i could what you told me but the problem is that i HAVE to make this project in Turbo C++ only and i know that this is ancient and i will go to hell for using it but plz try to help me with this project with the turbo Compiler
-
Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.
void search()
{char str2[100];
char str[100];
cout<<"write some text to search:";
gets(str);
ifstream i;
i.open("stud.txt");
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
}
getch();
}
You need to do the same thing for each file. This means breaking up your function into multiple functions. Hint: the new function would start at ifstream.
-
You need to do the same thing for each file. This means breaking up your function into multiple functions. Hint: the new function would start at ifstream.
-
Plz Help Me, I am trying to make a school project which is a search engine that takes input from the user and searches the entered word or sentence within the files available. There's no compile error in the program but the program is not searching correctly the file which has the data. If the entered string matches any sentence or string in the file stored then it should display the result. But the problem is that the strcmp or stricmp or any function which searches for a match is not working. I am using it with if & else statement for now... If the string is in the file then it should show found else not found but it only works if there is only one word in the file. If the file which is to be searched has lots of data off course in the text format it always shows not found but if it has only one word then it says found. Plz help Me If You Can. Also plz tell me how can i do the same thing which is to search for a particular entered word in more than one file like for example 10-20 files and then after searching it should display the whole line in the file or (files) found. Thanks For Your Help In Advance. I appreciate it ! Here's the code for searching the word in one file. Plz also tell me how to search for the entered word in multiple files.
void search()
{char str2[100];
char str[100];
cout<<"write some text to search:";
gets(str);
ifstream i;
i.open("stud.txt");
while(!i.eof())
{
i>>str2;if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
}
getch();
}
Hack Baba wrote:
Plz also tell me how to search for the entered word in multiple files.
Take your code from the
ifstream
declaration down and move to a separate function. Create a loop that iterates each filename you wish to process, calling the aforementioned function with each filename."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Think about the difference of using cout inside the loop and after the loop.