String 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.
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>>str;
}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.
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>>str;
}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.
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>>str;
}if(stricmp(str,str2)==0) cout<<"found !"; else cout<<"not found !";
getch();
}
You are reading the file and keyboard input into the same string.
-
You are reading the file and keyboard input into the same string.
Sorry for the incorrect edit. The string was str which was getting the input and the string which reads the file is different as you said in the code. I searched for the last word in the file and then it says found !. So that means it is working correctly but it's only searching for the last word in the file. What to do ?
-
Sorry for the incorrect edit. The string was str which was getting the input and the string which reads the file is different as you said in the code. I searched for the last word in the file and then it says found !. So that means it is working correctly but it's only searching for the last word in the file. What to do ?
Put the test for equivalency inside the while loop. Currently the while loop reads all strings, when it exits the last string is in the variable, which you then test for.
"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
-
Put the test for equivalency inside the while loop. Currently the while loop reads all strings, when it exits the last string is in the variable, which you then test for.
"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
-
Put the test for equivalency inside the while loop. Currently the while loop reads all strings, when it exits the last string is in the variable, which you then test for.
"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