#include <iostream>
using std::cout;

bool help = 0;
bool expl = 0;

int main(int argc, char *argv[])
{
for(int i=1; i + 1 <= argc; i++)
 {
 if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)
  {
   cout << "This is an experiment for parsing arguments.\n"
        << "Possible arguments are:\n"
        << "-h : Show this help.\n"
        << "-e : a short explanation.\n";
  help = 1;
  }
 else if((argv[i] == "e" | argv[i] == "--expl" ) && expl != 1)
  {
   cout << "This program uses a for loop to get i variable value.\n"
        << "The i variable is used with an if loop to check for arguments.\n";
   expl = 1;
  }
 else
  {
   cout << "Invalid argument: " << argv[i] << "\n";
  }
 }
return 0;
}

My problem is that I always get the "Invalid argument" message. Any hints?

On line 11 and 19 you cannot compare a char string using the == operator. Instead use strcmp http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html.

Line 11:

if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)

Should be:

if ((strcmp(argv[i], "-h") == 0) | (strcmp(argv[i], "--help") == 0)&&help != 1)

Do the same thing with line 19 and see if it works then.

You could also use std::string and then use the == operator to compare it with another string.

commented: Helpful +2

Thank you for stopping to help me.

Shouldnt You Be Using "||" For OR.

I mean

if((argv[i] == "-h" | argv[i] == "--help" ) && help != 1)

Should Be Actually

if((argv[i] == "-h" || argv[i] == "--help" ) && help != 1)

Right.

Good point!, he got me thicking strangely. But you cannot compare two arrays using the == operator.
So the correct way would be:

if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0)&&help != 1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.