Thread: Why my code crashes

  1. #16
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    No

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    I get this error when it tries to coimpile it :

    Code:
    strings.c(5) : Error: need explicit cast to convert
    from: char *
    to  : int
            if(strcmp(name, guess)==0) {
                                 ^
    strings.c(19) : Error: need explicit cast for function parameter 1 to get
    from: int *
    to  : char const *
    --- errorlevel 1

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int name[] = {"EDDIE","BEN","DAVE"};
    U are comparing with the int array which is not right. Perhaps it should have been a 2D string. And even more the strcmp string is expecting the char * not int * or char[][].

    So above code should be
    Code:
    char name[][20] =  {"EDDIE","BEN","DAVE"};
    And perhaps you now should change your code to suite he above statment

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() 
    {
        
     char name[][20] = {"EDDIE","BEN","DAVE"};
     int i;
     char guess[10];
     int correct = 0;
     char * p;
    
     printf("Enter a name in uppercase: ");
    
     while(!correct) 
     {
      fgets(guess ,10, stdin);
      p = strchr(guess, '\n');
      if ( p ) 
         *p = 0;
    
     for(i=0;i<3;i++)
     {
      if(strcmp(name[i], guess)==0) 
      {
       printf("Correct!\n");
       correct = 1;
       getchar();
       return 0;
      }
     } 
     printf("Try again: ");
     }
     getchar();
     return 0;
    }
    ssharish2005

  4. #19
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler tries to tell you that a pointer to an int is not a c-string.
    You could try to declare name like this
    Code:
    char name[][6] = {"EDDIE","BEN","DAVE"};
    Of corse you would need some kind of loop to find out if any of the names matches.
    Kurt

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    ssharish and kurt , thank you VERY much . That solves my problem perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When your code crashes
    By caroundw5h in forum C Programming
    Replies: 9
    Last Post: 06-19-2004, 09:40 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. code crashes on Solaris
    By watcher in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 07:58 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM