0

I want to do something simple: check what a parameter is.

void _tmain(int argc, WCHAR *argv[])
{



if(argv[4] == "-h");
{

        //do stuff
}


}

I am getting an incompatible error from WCHAR * to const char *.

Such a conversion question has been asked, but the answers I have found are many and not simple. I am a total newbie at C++.

What I am looking for is not pointers to some complicated functions, but rather the actual code to put into my program to make it work.

Just looking for something simple, straightforward, working code. Thanks!

ThePrince
  • 818
  • 2
  • 12
  • 26
  • is this what you're looking for? The second answer gives you a standard char to wchar function. http://stackoverflow.com/questions/8032080/how-to-convert-char-to-wchar-t – AlexLordThorsen Mar 01 '13 at 17:59

4 Answers4

1

You have two problems here:

  1. Conflating "pointer to char" with a logical concept of a string object. In C (and C++) a pointer to char (char*) is only a C-style string by convention. And by that I mean if you nul-terminate it (\0) and use the appropriate library methods. Because the pointer to char is not an actual string type, you are performing pointer equality rather than logical equality.

  2. Conflating wide characters (L"A") with a narrow characters ("A"). These are two distinct concepts, not likely to be helpful to you.

I suggest ditching the Unicode entry point:

/* do you really want C or C++? */
int main(int argc, char **argv)
{
    /* now we're "narrow" characters, which will be more natural for you */

And improving your tests (or using getopt):

    if (0 == strcmp(argv[4], "-h"))
    {
        /* argv[4] is equal to "-h" */

If you really wanted C++, you should be using std::string instead of older C-style strings.

user7116
  • 63,008
  • 17
  • 141
  • 172
0

try this:

#include <string>


void _tmain(int argc, WCHAR *argv[])
{
    typedef std::basic_string<WCHAR> string_type;
    static const string_type help( TEXT("-h") );

    if(argv[4] == help);
    {
        //do stuff
    }
...
}

I assume that the TEXT macro still exists - I haven't done any windows c++ programming in years.

I also assume that the -h stands for "help" :)

utnapistim
  • 26,809
  • 3
  • 46
  • 82
0
#include <string.h>
void _tmain(int argc, WCHAR *argv[])
{



   if( 0 == wcscmp(argv[4], L"-h") )
   {

        //do stuff
   }


}

That should do it.

Markus Schumann
  • 7,636
  • 1
  • 21
  • 27
  • I accepted this answer because it was the simplest. Several answers here worked as well so I had to select based on other factors! THANK YOU EVERYONE! – ThePrince Mar 01 '13 at 18:21
  • @user1784129 Might be the simplest solution, but using s.th. like `wcscmp()` may break portability of your code hard. That's not C++ standards ... – πάντα ῥεῖ Mar 01 '13 at 18:30
  • You should avoid using `_tmain` and `WCHAR` if you do not understand the larger, and far reaching, distinction. It will only cause you headaches. – user7116 Mar 01 '13 at 20:01
0

You cannot compare pointers to character arrays (no matter if WCHAR* or char*) to check for equality of their contents (the stuff these are pointing to in memory).

You could try s.th. like this:

if(std::wstring(argv[4]) == std::wstring(L"-h")) 
{
    // ...
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @user1784129 BTW: You shouldn't only look for getting a working solution in code (in the ['gimme teh codez'](http://meta.stackexchange.com/questions/136210/please-do-this-for-me-questions) manner), but getting answers explaining the background why s.th. works or not. – πάντα ῥεῖ Mar 01 '13 at 18:28