command line arguments problem

This is a discussion on command line arguments problem within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a program which accepts a set of numbers and sort them in descending order and ...

  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
    21,636
    By the way, why are you using the old parameter declaration syntax?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    You can't compare strings with the = sign. User strcmp().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Ninja'd
    Last edited by rags_to_riches; 02-03-2009 at 07:08 AM. Reason: mats beat me to it

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are both slow.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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 07:22 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:24 PM

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