comparing user input

This is a discussion on comparing user input within the C Programming forums, part of the General Programming Boards category; I'm writing a chat application and would like an action to happen when a user types a certain word in ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    comparing user input

    I'm writing a chat application and would like an action to happen when a user types a certain word in a predefined order.

    for example if the user types "/wave" it would output "User waves to everyone" and if they type "/wave user" it would output "User waves to user."

    I know how to compare the simple action input with the following code:
    Code:
    if(strcmpi(message,"/wave")
    {
       sprintf(buffer,"%s waves to everyone", "User");
    }
    But I don't know how to compare the of type of input.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Registered User immeraufdemhund's Avatar
    Join Date
    Dec 2002
    Posts
    12

    an idea

    here's what i'd do, use the function

    strstr(char *s1, char *s2); what it does is searches through s1 for s2. Have it do that how ever many different commands you have, if it returns something other than 0, then have it do that command. I had an example, but i dont know where. See if that helps. The strstr(s1,s2) is in string.h
    Was kostet Europe, ich kaufe es!

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What I would do is create a system where the commands are passed in a manner similar to that of argc and argv in main(), and then compare argv[0] for /wave and then if argc > 2 , etc etc.

  4. #4
    Registered User immeraufdemhund's Avatar
    Join Date
    Dec 2002
    Posts
    12
    argc and argv are used to store the path,program and anything past that...so in essence it's only used for the startup and options....isn't it? or are you saying something close to that. hmm, storing a string, then tokenizing it. seems kinda rough to me. One problem i can see with my idea is if say they forgot to add a space. like
    hello/wave
    i dont think strstr would find it. but if you tokenized everything like the previous person said (sorry can't see your name right now) then you'd be able to find it. size and speed wize i dont know how good that the previous would be. It would seem to me that if you had to tokenize everything, then use a switch command (is that even a C command???) would seem kinda rough. hmm... oh boy.
    {
    ...
    robert.brain = smoking;
    ...
    }
    Last edited by immeraufdemhund; 12-14-2002 at 10:28 PM.
    Was kostet Europe, ich kaufe es!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What I would do is create a system where the commands are passed in a manner similar to that of argc and argv in main(), and then compare argv[0] for /wave and then if argc > 2 , etc etc.
    I said 'similar' so yes, that translates to 'close to'. I have a feeling they are doing something where the command is the first word in the string, and aruments are seperated by spaces, (like most IRC clients), so all yo need ot do is make an array (or linked list if you want) with each word, seperated by a space put into the array/list and then do strcmp on the first word to see what command it is and then take the appropriate action. This is similar to how most IRC clients do it I would assume.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Is the included code even close to what you guys are suggestong? Could post a small snippet showing your solution?

    Code:
    #include <string.h>
    #include <stdio.h>
    
    #define MAX_ACTIONS 2
    void CheckForAction(char message[80]);
    int main(void)
    {
       char message[] ="/wave", message1[] = "/whisper User_2";
    
       CheckForAction(message1);
       getchar();
       return (0);
    }
    void CheckForAction(char message[80])
    {
       char *action[MAX_ACTIONS] = {"/wave", "/whisper"};
       char temp[2][100] = {{""},{""}};
       char *ptr = NULL;
    
       ptr = strtok(message, " ");
       strcpy(temp[0],ptr);
       ptr = strtok(NULL," ");
       if(ptr != NULL)
          strcpy(temp[1],ptr);
       if(strcmp(temp[0], "/wave") == 0)
          printf("%s waves to everyone\n", "User_1");
       if(strcmp(temp[0], "/whisper") == 0 && strcmp(temp[1],"") == 0)
          printf("%s whispers to the crowd\n", "User_1");
       else
          printf("%s whispers to %s\n", "User_1", temp[1]);
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 09:23 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM

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