Re: I need help with strings
You cannot compare character arrays with the comparison operator ("=="). You must use some type of str function like strcmp() or strncmp(). Or, better yet, look up std::string. Standard strings are a much better way to hold string data than the old C style character arrays. If you use standard strings then you will be able to use operator==().
Re: I need help with strings
that makes alot more sense, so basically i can only use the comparison operator with int variables if im not mistaken?
Re: I need help with strings
Quote:
Originally Posted by coding_delight
that makes alot more sense, so basically i can only use the comparison operator with int variables if im not mistaken?
Not true. You can use the comparison operator with any POD data type, or any non-POD data type that has overloaded the comparison operator. You cannot, however, use it to compare arrays of any type. Caveat: You must be careful when comparing floats and doubles due to a computers inability to store irrational numbers accurately.
Re: I need help with strings
You can use the comparison operator with any type or class for which the operator is defined - ints, doubles, pointer addresses etc. Your char array, however, is a collection of variables, so it doesn't work.
If I am not mistaken, an array can be seen as a pointer to the first of a series of consecutive objects, so when you use == on an array it checks the value of the pointer, not the value of the objects in the array.
Re: I need help with strings
Quote:
Originally Posted by Hnefi
If I am not mistaken, an array can be seen as a pointer to the first of a series of consecutive objects, so when you use == on an array it checks the value of the pointer, not the value of the objects in the array.
That is correct, hence the reason coding_delight's comparison was always failing (the two pointers will never be equal).
Re: I need help with strings
Quote:
Originally Posted by sszd
Not true. You can use the comparison operator with any POD data type, or any non-POD data type that has overloaded the comparison operator. You cannot, however, use it to compare arrays of any type. Caveat: You must be careful when comparing floats and doubles due to a computers inability to store irrational numbers accurately.
thanks for your help sszd, i guess this means i have tok go back and read a few string tutorials. I'm also sorry if i posted on the wrong discussion because this discussion seems to be for the advanced programmers.
Re: I need help with strings
thanks to hnefi also, i just saw your post.
Re: I need help with strings
Quote:
Originally Posted by coding_delight
I'm also sorry if i posted on the wrong discussion because this discussion seems to be for the advanced programmers.
The only stupid questions are the ones you didn't ask (even if some people will pretend otherwise). If only advanced programmers were allowed to ask questions, where would all we amateurs turn?
Good luck with your progging.
Re: I need help with strings
Your question is a perfect case-in-point example of why you should prefer to use the C++ <string> library over raw C-Style character arrays whenever possible. (Certainly when just learning the language anyway)
The following is modified from the original post (untested) - All changes highlighted in bold.
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string secret_code = "comehere";
string guess;
cout << "please enter the secret code to enter: \n";
cin >> guess;
if (guess == secret_code)
{
cout << "please enter programmer\n\n\n";
}
else
{
cout << "that is incorrect, go home...\n\n\n";
}
return 0;
}
You should notice that the == operator works as expected with C++ strings.
Re: I need help with strings
Quote:
Originally Posted by Bench_
Your question is a perfect case-in-point example of why you should prefer to use the C++ <string> library over raw C-Style character arrays whenever possible. (Certainly when just learning the language anyway)
The following is modified from the original post (untested) - All changes highlighted in bold.
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string secret_code = "comehere";
string guess;
cout << "please enter the secret code to enter: \n";
cin >> guess;
if (guess == secret_code)
{
cout << "please enter programmer\n\n\n";
}
else
{
cout << "that is incorrect, go home...\n\n\n";
}
return 0;
}
You should notice that the == operator works as expected with C++ strings.
WOWWWWWW!!!:) thank you so much Bench, this forum is awesome, it works perfectly. So one last question, what is the use of C-style characters? are there any situations in which the raw c-style does indeed come in handy? I'm also curios as to know how the string library functions, im guessing it involves typecasting? keep on filling me up with knowledge i really aprreciate this guys:)
Re: I need help with strings
There are some useful things you can do with C strings such as:
when you want only 4 characters for a password or something
when you want to convert a char or string into an int
(only a C string function can do that)
other cool functions like to check if a char is lower or upper case, alphabet or number
Ahh and I'm afraid that's all I know :) .
Re: I need help with strings
Quote:
Originally Posted by redherring
There are some useful things you can do with C strings such as:
when you want only 4 characters for a password or something
when you want to convert a char or string into an int
(only a C string function can do that)
other cool functions like to check if a char is lower or upper case, alphabet or number
Ahh and I'm afraid that's all I know :) .
Which you can accomplish all of the above with std::string anyway. As far as how, that's left as a exercise for the reader ;)
Re: I need help with strings
Quote:
Originally Posted by redherring
There are some useful things you can do with C strings such as:
when you want only 4 characters for a password or something
You mean like string::substr()?
Quote:
when you want to convert a char or string into an int
(only a C string function can do that)
You mean to use the string::c_str() function when the calling function needs a const char *?
Quote:
other cool functions like to check if a char is lower or upper case, alphabet or number
???
A char is not a string. Anyway, you can get the individual characters of a std::string by using operator [] (just like an array).
Seems like you need to do a little better research, since all of these things can be done with string.
Regards,
Paul McKenzie