Thread: Determining if command line argument begins with first letter in 2d Array - HELP!

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

    Determining if command line argument begins with first letter in 2d Array - HELP!

    I am pretty new to this, and am currently lost on this. I've tried several things, but I just cannot get it to work.

    The program works for the most part: for instance, if I type Dog as my argument, Deadlift and Dumbbell will appear; however, it will also say that no word begins with the letter D. The second if statement is appearing regardless, even if there is a word in my array with the same first letter.

    *confused*

    Code:
    #include <stdio.h>
    
    #define WORDS 5
    
    int main(int argc, char *argv[])
    {
            char list[WORDS][40]={"Deadlift", "Squat", "Lunge", "Dumbbell", "Press"};
    
            int i, j;
    
            for (i=1;i<argc;i++)
            {
                    for (j=0;j<WORDS;j++)
                            if (argv[i][0] == list[j][0])
                            {
                                    printf("%s starts with the same letter\n", list[j]);          
                            }
    
                    if (argv[1][0]!=list[1][0])
                    {
                            printf("No letters begin with %s \n", argv[i]);
                    }
            }
            return 0;
    }
    Any help would be amazing!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Code:
                    if (argv[1][0]!=list[1][0])
    Why does this always look at the [0] letter of the [1] word?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    I am really not sure. I never fully got a grasp on one dimensional arrays, so 2d isn't too easy to understand for me right now. I have put all sorts of things in those brackets trying to solve this problem.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why do people insist on doing things the hardest way possible?

    Code:
    if (argv[i][0] == list[j][0])
     {
       printf("%s starts with the same letter\n", list[j]);          
     }
    else
     printf("No letters begin with %s \n", argv[i]);

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You get a double response because your second conditional statement is inside the loop...
    You can't say there are no words with a given letter until both loops complete without a positive result.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Yeah, I have tried taking it out of the loop, but then it just returns "there are no words that begin with (null). I am guessing it has something to do with the fact that I am trying to print a character, and I am using %s for a string. This is what is confusing me the most =(

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    And thank you for the else statement suggestion..that seems like the most simple way to do it, lol; however, I STILL can't get it to work. The output equals the number of entries in my 2d array. For instance, with D as the argument, the output was:

    Deadlift begins with the same letter
    No letter begins with D
    No letter begins with D
    Dumbbell begins with the same letter
    No letter begins with D

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's only after going through the entire word list printing "whatever begins with the same letter" are you in a position to say that NONE did, if that's that case. So you should put a flag inside that loop that gets set whenever a word is found. Then at the outside of that loop at the end, if that flag has never been touched, you can print out that "No letter begins with xxx"

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Reread the posts that came before:
    Quote Originally Posted by CommonTater View Post
    You can't say there are no words with a given letter until both loops complete without a positive result.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    It's really clear to me the OP needs to UNDERSTAND how to solve his problem using pencil and paper, and not code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of command line argument
    By natrajdreams in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 11:07 AM
  2. Variable Array Size From Command Line Argument?
    By Cell in forum C Programming
    Replies: 6
    Last Post: 03-30-2009, 09:08 PM
  3. Determining whether stdin is redirected via command line..
    By nightdancer707 in forum C Programming
    Replies: 3
    Last Post: 10-01-2004, 02:35 PM
  4. command argument line
    By Tonyukuk in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2003, 09:10 AM
  5. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM