Follow us on Twitter!
Capitalism is an Island of wealth in a sea of poverty
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 23
Guests Online: 22
Members Online: 1

Registered Members: 82903
Newest Member: Piriformis
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-08-09 20:30
I'm trying to set up a C++ program to collect command line arguments, and run different code based on the argument, but every time I run the code, no matter which argument I use, I get the results from the final else statement: Invalid argument.
I'm assuming the way I'm going about comparing the characters is wrong, or else it's the structure of my conditional statements.
here's the code (haven't gotten to the real code yet, I wanted to get command line args working first...)
Code

#include <iostream>
#include <fstream>
using namespace std;

void properUse()
{
    cout << "Usage:\n";
}

int main(int argc,char* argv[])
{
    if(argc==1)
    {
        properUse();
        return 1;
    }
    else if(argv[1]=="h")
    {
        properUse();
        return 0;
    }
    else if(argv[1]=="u")
    {
cout << "success, \"u\"";
    }
    else if(argv[1]=="d")
    {
return 0;
    }
   else if(argv[1]=="b")
   {
return 0;
    }
    else
    {
        cout << argv[0] << ": Invalid argument.\n";
        properUse();
        return 2;
        }
}





I tried to use single quotes, but I get an error about C++ not being able to compare pointers to integers.
any advice would be appreciated.
the only info I could find on google was on how to output the command arguments in a loop.

Edited by on 26-08-09 20:34
Author

RE: C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-08-09 22:16
to compare strings use strcmp() or strncmp()

take a look at the reference for further info


Author

RE: C++ command line argument help

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 26-08-09 22:23
You didn't say how you were entering your arguments, but here's what I'm guessing your problem is:

It gives you an error when you use single quotes because each element of your argv array is a string: argv[0] is your first argument, not the first character of your argument. argv is an array of character arrays.

You can then either make an array and set it equal to each argument, or you can use double array notation (e.g. argv[0][1] is the second character of your first argument).

EDIT: Here's an example to compare to (scroll to the very bottom). It's in C, but it should be about the same:
http://www.hellbo. . .223_c.html


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 26-08-09 22:43
ynori7 http://halls-of-valhalla.org
Author

RE: C++ command line argument help

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 26-08-09 22:27
Longbow wrote:
to compare strings use strcmp() or strncmp()

The == operator is overloaded in C++ to compare strings.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-08-09 03:56
I know they are strings, that much I gathered from googling, and the first element in the array is the name of the program, thus, I want the program to accept 2 command line arguments, one as the program name, and one, which was originally to be -h or -u etc, but the program always assumes that the command line args don't match anything, and default to the final option, which is to say you used an invalid argument.
Author

RE: C++ command line argument help

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 27-08-09 04:45
Because "-h" is not equal to "h". The dash is part of the string.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-08-09 14:20
no, now it crashes as soon as I run it.
I changed the code a bit. it still doesn't work, but it's ouputting that I used the argument I should have even though it doesn't work.
This is why I don't work with Windows.
here's the new code (I added the dashes back. I had removed them to see if that was the problem originally, but it wasn't so I added them back)
Code

#include <iostream>
#include <fstream>
using namespace std;

ofstream cipherFile;
void properUse()
{
    cout << "Usage:\n";
}

int main(int argc,char* argv[])
{
    if(argc==1)
    {
        properUse();
        return 1;
    }
    else if(argv[1]=="-h")
    {
        properUse();
        return 0;
    }
    else if(argv[1]=="-u")
    {
cout << "success, \"u\"";
    }
    else if(argv[1]=="-d")
    {
return 0;
    }
   else if(argv[1]=="-b")
   {
return 0;
    }
    else
    {
        cout << "Invalid argument: \"" << argv[1] << "\"\n";
        properUse();
        return 2;
        }
}




it output 'Invalid argument "-u"', despite this bit of code:
Code

  else if(argv[1]=="-u")
    {
cout << "success, \"u\"";
    }





Edited by on 27-08-09 14:39
Author

RE: C++ command line argument help

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 27-08-09 16:36
Edit to what I said earlier: You need to include <string> for == to work with strings. Otherwise you can use strcmp as was suggested earlier. Try changing that and it should work.

EDIT:
This is why I don't work with Windows.

This has nothing to do with Windows.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 27-08-09 16:38
ynori7 http://halls-of-valhalla.org
Author

RE: C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-08-09 19:39
I can't help that I don't know what I'm doing, I've never worked with command line arguments before, except to output them in a loop, which is completely useless.
it works with strcmp, though. thank you.
Author

RE: C++ command line argument help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 28-08-09 17:58
Possibly. I'd never heard of the strcmp command, though. If I had known that before, I probably wouldn't have even needed help (yet).
Also, I have no idea how I screwed up my code before, but the first time i tried it with the strcmp command, the program immediately crashed with status -10756134 or something like that.
I have the command line args all working as they should, now, so naturally, the program is quickly beginning to grow beyond my ability to code and debug. It's great for learning, though...
Author

RE: C++ command line argument help

eXXon
Member



Posts: 141
Location:
Joined: 16.09.06
Rank:
Newbie
Posted on 02-09-09 04:59
dont know if this helps but this is what I usually use when taking arguments.
Code


int main( int argc, char* argv[] )
{
  int x = 3; //number of arguments passed
   
  if( argc < x )
  {
    help();
  }
  else
  {
    if( argv[1][0] == '-' )
    {
   if( argv[1][1] == 'h' )
   {
     help();
        }
   else if( argv[1][1] == 'e' )
   {      {
          if( argv[2][0] == '-' )
          {
       if( argv[2][1] == 'b' )
            {
                //do something
            }
       else if( argv[2][1] == 'o' )
            {
               //do something
            }
            else
       {
         help();
       }
         }
        else
        {
          help();
        }
      }
      else if( argv[1][1] == 'd' )
      {
   if( argv[2][0] == '-' )
       {
          if( argv[2][1] == 'b' )
          {
            //do something
          }
     else if( argv[2][1] == 'o' )
          {
             //do something
          }
     else
     {
      help();
     }
       }
       else
       {
          help();
       }
     }
   }
   else
   {
      help();
   }
  }
  return 0;
}






so it's just a bunch of conditional statements but it's just a way to get around the comparing strings part.. i know it might seem messy but it's actually easier for me to find what i'm looking for this way.

by the way sorry if the indentation is all messed up..


i110.photobucket.com/albums/n85/exxonfs/eXXon2.jpg
exxonfs@hotmail.com