Thread: catching from command line.

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    catching from command line.

    hey guys, i've got an issue with my code.. I'm trying to get the program to output names when the user types in an "@" symbol.. here's the code which isnt working.. any help would be appreciated.

    Code:
     int main(void) {
       char word[20];
    
       scanf("%s", &word);
       char names[1] =  "@";
       printf("names = %s \n", names);
    	   if (word == names){
    		   printf("Joe, Dave, Larry");
    	   }
    	   printf("wrong answer, you typed in %s\n", word);
       }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A string is zero or more characters terminated by a null zero. Where's the room in "names" for the null? Also, you can't compare strings in C with the == sign. You will need something like strcmp. Furthermore, since the name of an array is a pointer to the first element, you need to get rid of the & on that particular scanf call. You should also read the FAQ about reading input from a user, for a better method of doing so.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hi this could work for you

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    int main(void) 
    {
       char word[20];
       int i;
       scanf("%s", &word);
       char names =  '@';
       printf("names = %c \n", names);
    
       for(i=0;i<20;i++)
       {
    	   if (word [i] == names)
         {
    		   printf("Joe, Dave, Larry");
           exit(0);
    	   }        
        } 
           printf("wrong answer, you typed in %s\n", word);    
      
    return 0;
      }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    With the exception of the item in red which shouldn't be there.
    Quote Originally Posted by shwetha_siddu View Post
    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    int main(void) 
    {
       char word[20];
       int i;
       scanf("%s", &word);
       char names =  '@';
       printf("names = %c \n", names);
    
       for(i=0;i<20;i++)
       {
    	   if (word [i] == names)
         {
    		   printf("Joe, Dave, Larry");
           exit(0);
    	   }        
        } 
           printf("wrong answer, you typed in %s\n", word);    
      
    return 0;
      }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by itCbitC View Post
    With the exception of the item in red which shouldn't be there.
    i am sorry i directly copied that code from original post.

    Regards,
    Shwetha

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    thanks for the answer guys, it helped a lot.

    I have a somewhat related follow up question though.. if i'm trying to read from the command line, how would I be able to store the arguments as a string..

    For example, say if i was doing ./a.out d03dfs, how would i be able to store "d03dfs" as a string?

    i've tried
    Code:
    char input[50];
    int i;
       for(i = 0; i < argc; i++)
        	printf("arg %d: %s\n", i, argv[i]);
    input[0] = argv[1];
    printf("you again typed in %s \n", input);
    but it does not seem to work. Any ideas? Thanks again.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You need to use strcpy() for doing that.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    tried doing that too, using

    Code:
        int desti = 0;
        for(i = 0; i < argc; i++){
        strcpy(input[desti],argv[i]);
        desti++;
        }
    so that the value of all the arguments would be put into input, but no luck

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Uss_Defiant View Post
    tried doing that too, using

    Code:
        int desti = 0;
        for(i = 0; i < argc; i++){
        strcpy(input[desti],argv[i]);
        desti++;
        }
    so that the value of all the arguments would be put into input, but no luck
    First argument of strcpy() is a pointer; do you think input[desti] is a pointer??
    Increment desti by the length of the string before starting the next iteration as incrementing by one overwrites what was there before.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Quote Originally Posted by itCbitC View Post
    First argument of strcpy() is a pointer; do you think input[desti] is a pointer??
    Increment desti by the length of the string before starting the next iteration as incrementing by one overwrites what was there before.
    sorry that i'm being dense, but how would you create a pointer to input[desti]? I'm very new at this an examples would be helpfull.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You wouldn't. You would just use the name of the array. The name of an array can be used almost like a pointer to its first element. They are not the same thing, but can be used in a similar fashion most of the time.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    thanks guys, i got it working using

    Code:
         for(i = 1; i < argc; i++){
        	strcat(input,argv[i]);
        }
    
     	printf("argc is %d \n", argc);
        printf("you typed in %s \n", input);
    my issue now is that certain things mess up the code

    for example, "./a.exe 12 / 3" will yield "you typed in 12/3"
    but
    "a.exe12 * 3" will yield "you typed in 12a.exeasdfsrc3"

    the argc value from the first is 4 (expected), but from the second is 6.

    Anyone got any ideas?

    Thanks again.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Wild cards, pipes, etc, are handled differently by your shell. A * to your program is just a *, but to your shell it may be grabbed before it reaches the list of command line args, and swapped out for something else.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Ah i see, is there any way to code my program to ignore these expressions?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It happens before your program gets the arguments. You'd have to make sure the Shell wasn't munging up your arguments, which is a bit tricky. You could try using quotes around arguments, but really you'd want to research your particular shell / environment. Using quotes changes how arguments arrive as well.

    myprog hello world
    argv0 argv1 argv2

    myprog "hello world"
    argv0 argv1


    So, that's something else you'd have to deal with.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. catching keypresses
    By elmutt in forum Windows Programming
    Replies: 10
    Last Post: 08-01-2007, 09:24 AM
  2. Catching key strokes in a ListView
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 07-28-2006, 02:40 AM
  3. OST2: Neglection catching up to me
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-24-2004, 12:44 PM
  4. Catching bad input
    By Asmodan in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 06:24 PM
  5. Catching the elusive winsock
    By jinx in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 08:19 PM