Assigning a character to an argument?

How can I assign a character to argv?

In a broader scope my problem is that I need to use input from the command line (as opposed to using cin) to determine the action of an if statement.

For example, if(operation == a). How can i set up the variable "operation" so that it gets assigned to an argument from the command line? Simply writing
operation = argv[1] won't work, so how do you do it? or do i have the total wrong idea?

I've been scouring the web all night but being fairly new to C++ i'm sure i'm using an incorrect term or something and as of yet haven't been able to find a plainly written solution/explanation.

Last edited on
You can directly use argv[1] instead of the variable operation.
A simplest way is to declare variable operation as having type std::string. For example

std::string operation( argc[1] );
Thanks for the quick response

I've tried substituting the "operation" variable with argv[1] directly but that gives me an error due to the incompatibility of char* and char.

Any ideas?
Show your code. What are you trying to do?
You are overlooking the fact that argv[] is an array. Each element of that array is pointer to a character string. That's why its type is given as char *.

That's why the previous suggestion was to use type std::string rather than type char.

You could do everything using the older c-strings but then you need to get involved with extra work, such as strcpy() to copy, or strcmp() to compare and so on.

The std::string allows copying one string to another with a simple = operator:
    std::string parm1 = argv[1]; 

and comparing strings with the == operator:
    if (parm1 == "a")

Last edited on
Aah, i see. Thanks Chevril.

Vlad, it turns out i'm an idiot. I've set it up properly and that did the trick. Thanks again
Topic archived. No new replies allowed.