CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2007
    Posts
    11

    Unhappy I need help with strings

    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.

  2. #2
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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==().
    Last edited by sszd; July 9th, 2007 at 04:02 PM.

  3. #3
    Join Date
    Jul 2007
    Posts
    11

    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?

  4. #4
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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.
    Last edited by sszd; July 9th, 2007 at 04:10 PM.

  5. #5
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    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.
    Errare humanum est, ergo non sum humanus.

  6. #6
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    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).

  7. #7
    Join Date
    Jul 2007
    Posts
    11

    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.

  8. #8
    Join Date
    Jul 2007
    Posts
    11

    Re: I need help with strings

    thanks to hnefi also, i just saw your post.

  9. #9
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    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.
    Errare humanum est, ergo non sum humanus.

  10. #10
    Join Date
    Jan 2007
    Posts
    94

    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.

  11. #11
    Join Date
    Jul 2007
    Posts
    11

    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

  12. #12
    Join Date
    Jul 2007
    Location
    Canada, Ontario
    Posts
    1

    Smile 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 .
    Last edited by redherring; July 9th, 2007 at 09:15 PM.

  13. #13
    Join Date
    May 2007
    Posts
    811

    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

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    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()?
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured