strcmp question

This is a discussion on strcmp question within the C Programming forums, part of the General Programming Boards category; Part of my file: Code: int main(void) { int done = 0; char line[300]; do { printf("> "); fgets(line, 300, ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    20

    strcmp question

    Part of my file:

    Code:
    int main(void)
    {
    
       int done = 0;
       char line[300];
    
       do
       {
          printf("> ");
          fgets(line, 300, stdin);
          line[strlen(line) - 1] = '\0';
    
          if(strcmp(line, "area items") == 0)
          {
             areaItems(current, startPtr);
          }
    
          if(strcmp(line, "commands") == 0)
          {
             commands();
          }
    
    //...etc
    
       }while(! done);
    
       return(0);
    }
    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
    Location
    Melbourne, Australia
    Posts
    426
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

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


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

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

    Code:
    #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);
    	
    	getchar();
    	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
    Posts
    20
    Thanks! Got sscanf to work

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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, 09:39 PM
  2. Strcmp Question - C
    By Dr.Zoidburg in forum C Programming
    Replies: 8
    Last Post: 01-22-2009, 09: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


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