Thread: not taking command line arguments

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    not taking command line arguments

    IDE : Dev C++

    I'd like to add command line argument in my project but couldn't make it..
    so, I decided to create and check the basic program..

    The problem is, its not taking arguments.

    Code:
    # include <stdio.h>
    
    int main (int argc, char* argv[])
    {   
        int i;
        printf("number of arguments: %d\n",argc);
        printf("name of program %d\n",argv[0]);
        
        for (i=1; i<argc; i++)
          printf("user val = %d : %s\n",i,argv[i]);
          
        system("pause");
        return 0;
    }
    
    can you tell me what went wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a close look at line 7 ....

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @ComonTater: Oops! that's a typo.. %s
    but still its the same.

    Its not giving me change to enter. ..

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I have to put the arguments using an IDE option but its not happening at command line

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    # include <stdio.h>
    
    int main (int argc, char* argv[])
    {   
        int i;
        printf("number of arguments: %d\n",argc);
        printf("name of program %s\n",argv[0]);
        
        for (i=1; i<argc; i++)
          printf("user val = %d : %s\n",i,argv[i]);
          
        system("pause");
        return 0;
    }

    Your code.... when compiled on pelles C .... and run from a command window...


    not taking command line arguments-cmdlines-png

    It works just fine... run it outside the IDE... you'll see.

    Also DEVC++ is way out of date... maybe you should consider updating to Pelles C or MinGw...
    Last edited by CommonTater; 11-03-2011 at 06:03 AM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Then I think. there is a problem with the compiler/ IDE.
    Thanks for helping

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by suryak View Post
    Then I think. there is a problem with the compiler/ IDE.
    Thanks for helping
    Pelles C ... up to date and free.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by suryak View Post
    Then I think. there is a problem with the compiler/ IDE.
    Thanks for helping
    By default, if running your program within an IDE, command line arguments are not supplied.

    There is usually some IDE-specific configuration option (found through menus or setting dialogs) that allows you to specify command line arguments that the IDE will supply to your program. You need to find that option and use it, (and also ensure any settings are saved into configuration files - with some IDEs the setting is not saved, and needs to be re-entered every time you run your program).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Can you tell me why this error is coming.. I was rebuilding all my code so, this was pointed out.
    20 D:\sudoku\main.c [Warning] passing arg 1 of `input' from incompatible pointer type

    This is something I did with the code
    Code:
    int input (char* argv[]) // input function declared this way
    
    input (argv[1]); // in one file

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can't do that. The command line arguments are constants inside the program.

    If you want to get input from the user use an internally declared variable... like this...

    Code:
    char userval[100];
    
    if (! strlen(argv[x]))
      { please enter argument %d : ",x);
        scanf(" %100s",userval); }
    else
      strcpy(argv[x],userval);
    
    printf("%s", userval);
    Last edited by CommonTater; 11-03-2011 at 07:09 AM. Reason: alignment

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    argv[1] is of type char *. input() expects an argument of type char ** (note the extra asterix) or, nominally, an array of char *.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I'd rather take a string as input and pass it. its working!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Line Arguments
    By DuckCowMooQuack in forum C Programming
    Replies: 5
    Last Post: 03-30-2011, 03:33 PM
  2. command line arguments
    By KBriggs in forum C Programming
    Replies: 7
    Last Post: 06-25-2009, 01:57 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Taking inputs at Command Line
    By Stephenf in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2005, 02:33 AM
  5. command line arguments
    By singhhome in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 08:54 AM

Tags for this Thread