question about strncmp

This is a discussion on question about strncmp within the C Programming forums, part of the General Programming Boards category; I'm using strncmp to return a 1 if the user has typed in recent: Code: return ((strncmp(checkvalue, "recent", 1) == ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    question about strncmp

    I'm using strncmp to return a 1 if the user has typed in recent:

    Code:
    return ((strncmp(checkvalue, "recent", 1) == 0)||(strncmp(checkvalue, "RECENT", 1) == 0)||(strncmp(checkvalue, "Recent", 1) == 0));
    But if the user types in anything that begins in R it still returns 1, is this becuase strncmp is only looking at 1 letter, if so how can I change this?

    Many Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Use strcmp not strncmp. Strncmp checks only up to a given length; in your case the first character.

    Edit: your code would look cleaner, and work better if you converted the string to upper or lower case first. That way if the user enters "REcent" by accident it would still be valid.
    Last edited by mike_g; 03-22-2008 at 01:13 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I've just figured out thats what the number 1 was doing in that. How would i use strcmp, just the same but without the number at the end?

    Thanks Again

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yep.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by sweetorangepie View Post
    I've just figured out thats what the number 1 was doing in that. How would i use strcmp, just the same but without the number at the end?

    Thanks Again
    You should learn to use C-reference - it describes exactly what each parametr is, what possible return values are, and how you can investigate the error happend.

    Choose one that you like and start using it when you have a doubt how to call some standard function...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    i have now changed it to:

    Code:
    return ((strcmp(checkvalue, "recent") == 0)||(strcmp(checkvalue, "RECENT") == 0)||(strcmp(checkvalue, "Recent") == 0));
    but it is no longer returning a 1, what am i doing wrong?

    Thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    but it is no longer returning a 1, what am i doing wrong?
    Post the smallest and simplest compilable program that demonstrates the problem. For example, this works for me:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int isRecent(const char *checkvalue);
    
    int main(void)
    {
        const char *str = "Recent";
        printf("&#37;d", isRecent(str));
        return 0;
    }
    
    int isRecent(const char *checkvalue)
    {
        return strcmp(checkvalue, "recent") == 0
            || strcmp(checkvalue, "RECENT") == 0
            || strcmp(checkvalue, "Recent") == 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    This is the simplest version of what i'm trying to achieve:

    Code:
    int main (void)
    
    recent = show_recent(guess);
    				if (0 != recent){
    					printf("\n%s", recent_guess1);
    					printf("%s", recent_guess2);
    					printf("%s", recent_guess3);
    					printf("%s", recent_guess4);
    					break;
    				}
    
    int show_recent( char* checkvar )
    {
    	return ((strcmp(checkvar, "recent") == 0)||(strcmp(checkvar, "RECENT") == 0)||(strcmp(checkvar, "Recent") == 0));
    }
    Can anyone see why this is?

    Thanks

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Post the smallest and simplest compilable program that demonstrates the problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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