Thread: if argv

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    if argv

    I'm trying to use an if statement that runs if you have an argv called help, but it doesn't work.

    Code:
    #include <iostream>
    
    using namespace std ;
    
    int main(int argc, char* argv[])
    {
    
      if ( argc ) {
        if ( argc == "help" ) {
        cout << "You said " << argv[1] << " just then.\n" ;
        }
        else {
        cout << "What?\n" ;
        }
      }
    
    }
    This is, as far as I could tell, right, but it outputs 'What?' every time, regardless.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    that's not how you compare strings, you need to use something like strcmp

    argc is an int anyway
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    argc (argument count) is an integer representing the total number of arguments, including the name of the application

    argv is an cstring array with the arguments, argv[0] is the first argument (which will always be the name of your application)

    Code:
    // Check all arguments (skip the first) to see if any are "help"
    for (int i = 1; i < argc; ++i) {
    	if (string (argv[i] ) == "help")
    		cout << "Argument " << i + 1 << ": help\n";
    	else
    		cout << "Argument " << i + 1 << ": unknown\n";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM