Server Help

Non-Subspace Related Coding - help with strncpy

hellzlaker - Sun Aug 24, 2008 6:49 pm
Post subject: help with strncpy
Im working on something that if you type it, and it finds it in data and says something, but its not working

Code: Show/Hide
                string data="fuck";
                string input;
                cout<<"enter :";
                cin>>input;
               
                int input2 = (int)strncpy((char*)input.c_str(),data.c_str(),sizeof(data));
               
                if(input2==0)
                {
                                                                          cout<<"woo u said fuck";_P;
                }
also im trying to make something that you type a whole sentence like "hello jimmy how are you" and if it finds word jimmy it says something back
Bak - Sun Aug 24, 2008 10:52 pm
Post subject:
strncpy does string copy (cpy = copy)

if you want to compare strings do strncmp (cmp = compare)

if you want to find a substring do strstr
Snrrrub - Mon Aug 25, 2008 10:03 am
Post subject:
More importantly, you're using C++'s std::string class so you should avoid functions like strcpy and strcmp entirely. Instead of using strcpy, use the assignment operator (str1 = str2) and instead of strcmp, use the equality operator (if (str1 == str2)).

I strongly recommend using std::string instead of C-style strings where possible - you'll significantly reduce the likelihood of errors and you'll waste less time writing repetitive code.

-Snrrrub
hellzlaker - Mon Aug 25, 2008 11:26 pm
Post subject:
What should I use from the C++ std::string if I would like a program to find a string from a string

for an example:

user types in a sentance hello my name is john
program looks if user said hello and if he did it says hello john
k0zy - Tue Aug 26, 2008 3:16 am
Post subject:
find

http://www.cplusplus.com/reference/string/string/find.html
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group