Using getopt() - getting options

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nispe
    New Member
    • Oct 2007
    • 7

    #1

    Using getopt() - getting options

    Hi,
    This is probably a really easy one but its got me;

    Im using getopt() and having some issues geting the specified options into a form where I can use them. Here a sample of the code Im using which compiles fine:

    [CODE=c]
    int main (int argc, char *argv[])
    {
    char *number_games = NULL;
    int NUMBER_GAMES = 0;
    int eflag = 0;
    opterr = 0;

    while ((c = getopt (argc, argv, "n:")) != -1)
    switch (c)
    {
    case 'n':
    number_games = optarg;
    printf("number of games = %s\n", number_games);
    break;
    case '?':
    if (optopt == 'n')
    {
    fprintf (stderr, "Option -n requires an argument between 128 and 32768.\n");
    eflag = true;
    }
    }

    NUMBER_GAMES = *number_games; // need a way to do this
    printf("number of games = %d\n", NUMBER_GAMES);
    }
    [/CODE]

    The first print statement gives the number which has been specified along side the -n option such as -n1234 but the second one gives some different value (the address or something Im guessing). Is there anyway I can get the number stored in char *number_games so I can use it in a for loop?

    Thanks.
Working...