Click to See Complete Forum and Search --> : I need help with strings
coding_delight
July 9th, 2007, 03:46 PM
I am quite new to c++ and i'm having trouble with strings. I know how to declare strings through an array of characters but for some reason they dont seem to work properly. I made the following program to illustrate my problem with strings.
#include <iostream>
using namespace std;
int main ()
{
char secret_code [] = "comehere";
char guess [15];
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;
}
for some reason, once i input the answer to the code it skips to the else portion of code and fails to read the secret_code. I tried various methods such as using the cin.getline() function or the cin.get(). They all have failed. can someone please help me correct this code so i understand what im doign wrong, thanks.
sszd
July 9th, 2007, 03:59 PM
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==().
coding_delight
July 9th, 2007, 04:03 PM
that makes alot more sense, so basically i can only use the comparison operator with int variables if im not mistaken?
sszd
July 9th, 2007, 04:07 PM
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.
Hnefi
July 9th, 2007, 04:10 PM
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.
sszd
July 9th, 2007, 04:15 PM
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).
coding_delight
July 9th, 2007, 04:19 PM
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.
coding_delight
July 9th, 2007, 04:20 PM
thanks to hnefi also, i just saw your post.
Hnefi
July 9th, 2007, 04:23 PM
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.
Bench_
July 9th, 2007, 06:52 PM
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.
#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.
coding_delight
July 9th, 2007, 08:05 PM
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.
#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:)
redherring
July 9th, 2007, 09:10 PM
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 :) .
STLDude
July 9th, 2007, 10:23 PM
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 ;)
Paul McKenzie
July 9th, 2007, 11:06 PM
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()?
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 *?
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.