Did you use any punctuation with "Do you have a sword?" If you did, I don''t think it will recognize it. But then again, I haven''t used anything too useful in a while.
If that doesn''t work, you could just use
if(strstr(buffer, "sword") != NULL)
*goodness*
else
*badness*
It looks like you''re assigning the buffer variable to a TRUE or FALSE, which, while ok, clears your buffer...so try that out.
"TV IS bad Meatwad...but we f***in need it"
If you''re a girl under the age of 12, and you''re high on marijuana...don''t ride your bike. -TRUTH
Finding a single word within a string
Well you could switch from char to string, which has multiple find functions for searching for substrings.
so:
string buffer;
if(buffer.find("sword")!=npos)
{
//due stuff
}
-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document
so:
string buffer;
if(buffer.find("sword")!=npos)
{
//due stuff
}
-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document
How about std::string::find?
Of course, this will also find "swordfish", but it can be modified fairly easily to only find "sword".
Edit: Got beat to it
[edited by - desert fox on March 21, 2004 11:15:42 PM]
#include <string>#include <iostream>int main(){ std::string str; std::getline(std::cin,str); if (str.find("sword") != std::string::npos) std::cout << "Found it" << std::endl; else std::cout << "Didn't find it" << std::endl; return 0;}
Of course, this will also find "swordfish", but it can be modified fairly easily to only find "sword".
Edit: Got beat to it
[edited by - desert fox on March 21, 2004 11:15:42 PM]
Problem # 500
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2065: ''string'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2146: syntax error : missing '';'' before identifier ''buffer''
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2065: ''buffer'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(15) : error C2228: left of ''.find'' must have class/struct/union type
Error executing cl.exe.
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2065: ''string'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2146: syntax error : missing '';'' before identifier ''buffer''
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(10) : error C2065: ''buffer'' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Practice2\Pract2.cpp(15) : error C2228: left of ''.find'' must have class/struct/union type
Error executing cl.exe.
#include <iostream>#include <string>int main(){ using std::cout; using std::cin; using std::endl; string buffer; cout << "Player -> "; std::cin.getline(buffer, sizeof(buffer)); if (buffer.find("sword") != NULL) { cout << "Yes." << endl; } else { cout << "No." << endl; } return 0;}
Howdy, noticed you were still having trouble.
The "string" error is likely because you're using string instead of std::string
Your "strstr" error is that you are trying to assign a char * to a char [] which doesn't work; char [] is more or less a
The "string" error is likely because you're using string instead of std::string
Your "strstr" error is that you are trying to assign a char * to a char [] which doesn't work; char [] is more or less a
const char * -- you can't change the address it points to, even though it is still a pointer.
How strstr works is:<br>buffer [500] = [.|.|.|s|w|o|r|d|.|.|.|\0]<br>char * result;<br><br>result = strstr (buffer, "sword");<br> gets you:<br><br> [.|.|.|s|w|o|r|d|.|.|.|\0]<br>result --------^<br> </pre> <br><br>So result = "sword...", and points into the original buffer.<br><br>Hope this helps!<br><br>KB<br><br>(modified because code tags should not be used lightly) <br><br><SPAN CLASS=editedby>[edited by - kendallemm on March 21, 2004 11:44:15 PM]</SPAN>
std::getline(std::cin,buffer);
This need''s a newline command. After pressing enter, it waits for you to press another ''enter'' til it goes to the person''s response.
I thought there was something like cin.put(''\n'') or something relating..?
This need''s a newline command. After pressing enter, it waits for you to press another ''enter'' til it goes to the person''s response.
I thought there was something like cin.put(''\n'') or something relating..?
This is the same problem as parsing a GL extension string, except a little worse because there can be punctuation.
strstr and string::find will both give false positives on the sentence "What is your password?". When I had to do this with a GL extension string, I used an istream_iterator (istringstream) and a back_inserter to split the string up by words, and then you can do a std::find on the vector but you''ll have to deal with punctuation manually.
Using a regular expression will be more reliable, but you''ll have to find a regular expression library to link in. There is one in glibc on Linux.
strstr and string::find will both give false positives on the sentence "What is your password?". When I had to do this with a GL extension string, I used an istream_iterator (istringstream) and a back_inserter to split the string up by words, and then you can do a std::find on the vector but you''ll have to deal with punctuation manually.
Using a regular expression will be more reliable, but you''ll have to find a regular expression library to link in. There is one in glibc on Linux.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement