Thread: strcmp question

  1. #1
    Registered User
    Join Date
    Aug 2011

    strcmp question

    Part of my file:

    int main(void)
       int done = 0;
       char line[300];
          printf("> ");
          fgets(line, 300, stdin);
          line[strlen(line) - 1] = '\0';
          if(strcmp(line, "area items") == 0)
             areaItems(current, startPtr);
          if(strcmp(line, "commands") == 0)
       }while(! done);
    The code is working fine. However, I'd like the user to be able to type "grab butter" or "grab toast" or "grab [item]". Can I still use strcmp to read in grab but store the second word in a char name[10]? Or is there something much better to use than strcmp?

    Hope that makes sense. Thanks

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Melbourne, Australia
    You can use strtok to parse any command the user enters into a series of tokens. Then you can use strcmp on the first token to see if it's "grab", and then do whatever you want with the remaining tokens.
    while(!asleep) {

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    sscanf will do what you want.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    As quzah suggested, you would want an fgets / sscanf pair; which is the easiest way, IMO, to get user input. So something like this:

    #include <stdio.h>
    int main(void){
    	char inputBuffer[100]={0};
    	char word1[10]={0}, word2[10]={0};
    	printf("> ");
    	//grab entire lineof input
    	fgets(inputBuffer, sizeof(inputBuffer), stdin);
    	//Parse the line into, in this case 2 different strings seperated by spaces
    	//it could be any format you want, e.g. %s %d %s %c, ect.
    	sscanf(inputBuffer, "%s %s", word1, word2);
    	printf("Word1:%s Word2:%s ", word1, word2);
    	return (0);
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Thanks! Got sscanf to work

  6. #6
    Join Date
    Aug 2010
    Ontario Canada
    If you only care if the words are present... but arent worried about the order... you could use strstr() which might be faster...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about strcmp
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 03-07-2009, 10:39 PM
  2. Strcmp Question - C
    By Dr.Zoidburg in forum C Programming
    Replies: 8
    Last Post: 01-22-2009, 10:03 AM
  3. question about function strcmp() in C
    By thungmail in forum C Programming
    Replies: 5
    Last Post: 03-13-2008, 05:46 AM
  4. A question about strcmp...
    By krsauls in forum C Programming
    Replies: 6
    Last Post: 05-02-2007, 04:39 AM
  5. Question about strcmp
    By readerwhiz in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 05:18 PM

Tags for this Thread