Help with basic C++ question

Started by
29 comments, last by Lord_Evil 15 years, 10 months ago
I'm pretty new to C++ myself, but aren't people typing /quit not "/quit"?

In that case wouldn't it be

if (command != quit)

since command is already defined as a string?
Advertisement
Quote:In that case wouldn't it be

if (command != quit)

I fail to see what issue you are raising. He compares command to /quit (command != "/quit"). Where is the variable quit defined?
if you're using std::strings you would want to do

std::string command = "not quitting";while(command.compare("/quit" != 0) // Compare returns 0 if the strings are equal{        std::cin >> command; // I would use getline(std::cin, command) personally                if(command.compare("/help") == 0)        {                 help();        }        if(command.compare("look") == 0)        {                 look();        }        if(command.compare("talk") == 0)        {                 talk();        }}
Quote:Original post by rofseek
if you're using std::strings you would want to do


Why do you need to use compare?
You have to use compare because this isn't java. "/quit" is a character pointer to the compile time constant C string "/quit" it may or may not evaluate as equal to the std::string "/quit".
Quote:Original post by stonemetal
You have to use compare because this isn't java. "/quit" is a character pointer to the compile time constant C string "/quit" it may or may not evaluate as equal to the std::string "/quit".


operator==() is overloaded to work fine with a mixture of std::strings and C strings. What you can't do is compare two C strings directly. As long as at least one side of == is a std::string instance you are fine.

And AFAIK you have to use .equals() in Java, which is worse [grin]
Quote:Original post by rofseek
if you're using std::strings you would want to do

*** Source Snippet Removed ***

std::strings' operator==() already does this. i.e., besides readability, there is no reason against using string::compare() over operator== . (Then again, I personally find if(str1==str2) very readable by itself)

If you want to compare two C strings, you can always use C's strcmp()..
Quote:Original post by stonemetal
You have to use compare because this isn't java.

You're confused. It's with Java that you need to use compare or equals.
I'm a hobbiest programmer.. and i do most of my work with the Win32 API where it's much more convenient for me to use _TCHAR, _tcscmp, and do the memory management (mostly with the aid of boost::scoped_ptr, and boost::shared_ptr) than to type
#ifdef UNICODE
std::wstring s
#else
std::string s
#endif

everywhere in my source, and because of this i was ignorant of the fact std::string had an overloaded operator==() for plain c-style strings.
Quote:
I'm a hobbiest programmer.. and i do most of my work with the Win32 API where it's much more convenient for me to use _TCHAR, _tcscmp, and do the memory management (mostly with the aid of boost::scoped_ptr, and boost::shared_ptr) than to type
#ifdef UNICODE
std::wstring s
#else
std::string s
#endif

everywhere in my source, and because of this i was ignorant of the fact std::string had an overloaded operator==() for plain c-style strings.

This is now offtopic, but you are apparently also unaware you don't have to type that. You can do (essentially) this, once:
#ifdef UNICODEtypedef std::basic_string<char_t> tstring;#elsetypedef std::basic_string<char> tstring;#endif

and use tstring (or whatever you want to call it) all over in conjuction with a macro like T() for string literals, and c_str() to pass to Win32, and you don't have to waste your time with the programming overhead of managing the character memory and you get ANSI/Unicode toggle-ability.

This topic is closed to new replies.

Advertisement