1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Parsing command line arguments in C

  1. Apr 3, 2012 #1
    Here is the given assignment:

    Write a code that would
    parse the command line
    and display the user’s first /
    last name and (if given)
    his/her age. The ‘-age’ key,
    if present, could be the first
    or, alternatively, the third
    argument and indicates that
    the following parameter is
    age, as in the example
    shown. If the command
    line arguments are not
    valid, an error message to
    that effect should be
    displayed. (You may use
    string functions as needed.)

    My program works as long as the user is inputting arguments with the -age key but crashes if any other time. Could I get a hint as to what is wrong?

    Code (Text):

    #include <stdio.h>
    int main(int argc, char *argv[]){
      if(argc < 3 || argc > 5 || argc == 4)
        printf("Invalid command line arguements.\n");
      if(!strcmp(argv[2], "-age") || !strcmp(argv[4], "-age"))
        printf("Invalid command line arguements.\n");
      if(argc == 3){
        printf("Your first name is: %s\n", argv[1]);
        printf("Your last name is: %s\n", argv[2]);
      if(!strcmp(argv[1], "-age")){
        printf("Your first name is: %s\n", argv[3]);
        printf("Your last name is: %s\n", argv[4]);
        printf("Your age is: %s\n", argv[2]);
      if(!strcmp(argv[3], "-age")){
        printf("Your first name is: %s\n", argv[1]);
        printf("Your last name is: %s\n", argv[2]);
        printf("Your age is: %s\n", argv[4]);
      return 0;
  2. jcsd
  3. Apr 3, 2012 #2
    Can you provide an example valid arguments? It said the age argument could be first or third, but in your program, it looks like it's the third or fifth...so I'm a little lost.
  4. Apr 3, 2012 #3
    "if(argc < 3 || argc > 5 || argc == 4)"

    You said age if given, so that means age input is not necessarily expected right?
  5. Apr 4, 2012 #4
    Sorry, let me clarify a little. Valid input would look something like this:

    Code (Text):
    program.exe John Doe -age 43
    Code (Text):
    program.exe -age 56 Jane Doe
    Code (Text):
    program.exe Bob Smith
    Where program.exe is argument 0 and so on...
  6. Apr 4, 2012 #5
    If the age is not suppled, your program would attempt to read or write past the end of the argv array. Read your source code and attempt to find out where.

    This is something that is very important to avoid. Every time you access an array element that might not exist, check the array size (or bounds) first!
  7. Apr 4, 2012 #6
    Thanks for the feedback everyone, I got it to work! I really should have noticed the issue before.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook