Beginner to C, need help with command-line stuff..

This is a discussion on Beginner to C, need help with command-line stuff.. within the C Programming forums, part of the General Programming Boards category; So I've gotten kinda stuck. I'm trying to figure out this command-line arguments stuff. Basically if a user enters an ...

  1. #1
    bmc
    bmc is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Beginner to C, need help with command-line stuff..

    So I've gotten kinda stuck. I'm trying to figure out this command-line arguments stuff.

    Basically if a user enters an integer x in the command line after the program name, how can I use this integer in my main() function? I can use the number they put in.

    Heres a lil sample of the code

    for ( i = 0; i < argv[1]; i++ )
    die1 = rollDie();
    resultArray2[total - 1]++;


    but it won't compile, saying I can't compare an integer (i) to a pointer (argv[1]). I've read 4 tutorials on command-line stuff and I haven't found anything that helps.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    argv[1] is a string. You need to convert it to an integer, look up the functions atoi and strtol.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    for ( i = 1; i < argc; ++i )
    argc contains the number of command-line arguments. So if you want to do something with each argument then create the loop as illustrated above. argc will always be at least 1 because the first argument is always the name of the program that is running.

    Now if you want to actually use the value that was typed on the commnd-line you will have to convert it to an integer. There are other conversion functions, but atoi() will be sufficient as long as you don't enter some ridiculously huge number on the command line.
    Code:
    int x;
    for ( i = 1; i < argc; ++i )
       x = atoi(argv[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM
  4. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21