Argv[]

I need help with making an if statement that checks for if the argument when a function is run is either -encode or -decode. This is what I have so far. How would I go about fixing this? I'm confused as to how to ask the if statement to check if argv[1] == -encode and vice versa. Any help help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  1 #include <cctype>
  2 #include <iostream>
  3 #include <stdio.h>
  4
  5 using namespace std;
  6
  7 int main(int argc, char* argv[])
  8 {
  9     if (argv[] == encode)
 10         encode(string&, int);
 11     if (argv[] == decode)
 12         decode(string&, int);
 13 }
You need to provide more information on the problem, for example, what algorithm are you using to encode and decode your string?

Oh, and I think you're looking for something like this:

1
2
3
4
5
6
7
8
int main(int argc, char* argv[])
{
    const std::string message = "Hello!";
    if (argv[1] == "encode")
        encode(message);
    if (argv[1] == "decode")
        decode(message);
}
Last edited on
Well I technically haven't written the functions for encode and decode. But the user uses the argument
 
./prog -encode 4 //4 is a number that will be used in the encode function 

to tell the program to encode. Then the user types in a string and the encoder encodes it. I just couldn't figure out how to get the if statement to recognize the -encode part.
Oh, and I think you're looking for something like this:

Except argv is an array or C-strings and you can use the comparison operator== to compare C-strings, you need to use strcmp() to compare C-strings.

Topic archived. No new replies allowed.