Thread: Command Line Arguments Dilemma!!

  1. #1
    Registered User
    Join Date
    Sep 2005

    Command Line Arguments Dilemma!!

    I'm trying to create program that takes argument form the command line. e.g as below..k.out of course, is my command name..and the arguments follows..

    k.out A+ B C D E

    This should then print out : Pass: 3 Real:4 Highest: A+

    char* pass[] = {"A+", "A", "B","B-", "C+","C"};
    char* fail[] = {"D","F"};

    p is the number of arguments in pass[]
    Real is p plus number of arguments in fail[]

    Highest is the argument in pass[] with smallest index or if none,
    the argument with smallest index in fail[] or " " if no valid arguments.

    Here's what i've been able to do..and i got stuck..any advise or directions would be highly appreciated..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(int argc, char *argv[])
      char* passing [] = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-"};
      char* failing [] = {"D", "F"};
      int n =0;
      int pCount = 0;
      int fCount = 0;
      for (i = 1, i<argc; i++)
         if ( strcmp(argv[i], passing[] ==0) )
         if (strcmp(argv[i], failing[] == 0) )
      printf("Valid:%d, Passed:%d, Highest: %s\n",  n, p, h);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Are you paying attention at all to your compiler's messages? My sources say no. You can't pass an entire array of pointers to strcmp and expect it to loop through them all for you. It doesn't work that way. Hell, just looking at the function prototype for strcmp tells you that much.

    Apply some basic logic:
    for each command line argument
        for each passing grade
            compare this command line argument to this passing grade
                if it's a match, stop looking
        for each failing grade
            compare this command line argument to this failing grade
                if it's a match, stop looking
    Now, if you don't know how to do that, I'd suggest writing a program that will simply print out each one of the grades, passing and failling. Once you figure out how to do that, you'll know how to do the above.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Hey Quzah,
    Thanks for that simple algor...i'd use that frame to work it..i appreicate i said iw asn't sure how the comparison works with the arrays.I'd get my updated version up once i'm done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM
Website Security Test