Thread: strcmp

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    strcmp

    i keep getting an error with this script

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char color[10];
    	printf("What wire do you want to cut?\n");
    	printf("Red, blue or yellow?: ");
    	scanf( "%s", &color );
    if ( strcmp (color[10], "blue") == 0 ) {
    printf("Correct!!!\n");
    getchar()
    }
    else {
    printf("Sorry that was incorrect.\n");
    printf("Press enter to close.");
    getchar();
    }
    the error is "missing prototype for strcmp"

    can anyone help me?
    Last edited by Salem; 08-28-2010 at 10:28 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    #include <string.h>

    also

    if(strcmp(color,"blue") == 0)

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    thanks a ton

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    #include <string.h>

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char color[10];
    	printf("What wire do you want to cut?\n");
    	printf("Red, blue or yellow?: ");
    	//scanf( "%s", &color ); // Wrong. Use fgets. Wrong. Don't use & for arrays.
    	fgets(color, sizeof(color), stdin);
    	// MISSING INDENTATION
    	// Wrong: color[10] returns the 10th element
    	//of the array which doesn't exist and you cannot compare a char to a string.
    // if ( strcmp (color[10], "blue") == 0 ) {
    	if (strcmp(color, "blue") == 0)
    	{
    		// MISSING INDENTATION
    printf("Correct!!!\n");
    //getchar(); // Wrong: Missing ;
    }
    	// MISSING INDENTATION
    else {
    		// MISSING INDENTATION
    printf("Sorry that was incorrect.\n");
    printf("Press enter to close.");
    //getchar();
    }
    SourceForge.net: Scanf woes - cpwiki
    SourceForge.net: Pause console - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM