Thread: command line arguments problem

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    6

    command line arguments problem

    Hi,
    I have to write a program which accepts a set of numbers and sort them in descending order and if "-r" is included in the command line then it should sort the numbers in ascending order.The program i wrote isn't working plz tell me what is wrong with it and how to correct it
    Code:
    # include <stdio.h>
    
    main (argc, argv)
    int argc;
    char *argv[];
    {
      int ar[100];
      int i, j, n, flag, temp;
    
      printf ("usage: a.out for descending order\n");
      printf ("usage: a.out -r for ascending order\n");
      printf ("enter the number of elements in array\n");
      scanf ("%d", &n);
      printf ("enter the elements of array\n");
      for (i = 0; i < n; i++)
       scanf ("%d", &ar[i]);
      if (*argv == "-r")
        {
         for (i = 0; i < n; i++)
            {
             for (j = 0; j < (n-i-1); j++)
                {
                 if (ar[j] > ar[j+1])
                   {
                    temp = ar[j];
                    ar[j] = ar[j+1];
                    ar[j+1] = temp;
                   }
                }
            }
         printf ("the sorted array is \n");
         for (i = 0; i < n; i++)
         printf ("%d\n", ar[i]);
        }
    else
       {
        for (i = 0; i < n; i++)
           {
             for (j = 0; j < (n-i-1); j++)
                 {
                  if (ar[j] < ar[j+1])
                    {
                     temp = ar[j];
                     ar[j] = ar[j+1];
                     ar[j+1] = temp;
                    }
                 }
            }
        printf ("the sorted array is \n");
        for (i = 0; i < n; i++)
        printf ("%d\n", ar[i]);
       }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two things wrong:
    Code:
      if (*argv == "-r")
    First, you probably want argv[1], not *argv (which is argv[0] using alternative syntax).

    Second, you are comparing if argv[0] points to the string constant "-r" - which is very unlikely.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, why are you using the old parameter declaration syntax?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    6
    I tried by placing
    Code:
    If (argv[1] == "-r")
    and
    Code:
    if ((argv[1] == "-" )&& (argv[2] == "r"))
    in place of the previous if statement but still it doesn't seem to work

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't compare strings with the = sign. User strcmp().
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abhiii View Post
    I tried by placing
    Code:
    If (argv[1] == "-r")
    and
    Code:
    if ((argv[1] == "-" )&& (argv[2] == "r"))
    in place of the previous if statement but still it doesn't seem to work
    To compare strings, you need to use strcmp() [and note that it returns zero for "matches exactly").

    Or you could do
    Code:
    (argv[1][0] == '-' && argv[1][1] == 'r')
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Ninja'd
    Last edited by rags_to_riches; 02-03-2009 at 08:08 AM. Reason: mats beat me to it

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are both slow.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would probably be best to write:
    Code:
    if (argc > 1 && strcmp(argv[1], "-r") == 0)
    Comparing the characters directly would allow say, -rags_to_riches (which might be fine, but the usage instructions only mention -r), unless a check if argv[1][2] == '\0' is performed.
    Last edited by laserlight; 02-03-2009 at 08:22 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. C++ arguments problem
    By swanley007 in forum C++ Programming
    Replies: 9
    Last Post: 08-01-2006, 12:32 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM