A question about strcmp...

This is a discussion on A question about strcmp... within the C Programming forums, part of the General Programming Boards category; I'm having problems getting an equality with strcmp. Here is my code: Code: char myframe[frameheight][framewidth]; patch pptr; patch *p; p ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    A question about strcmp...

    I'm having problems getting an equality with strcmp. Here is my code:

    Code:
        char myframe[frameheight][framewidth];
        patch pptr;
        patch *p;
        p = &pptr;
        command cptr;
        command *c;
        c = &cptr;
        
        //system("PAUSE");
        int i;
        for(i = 0; i < sizeof(commandstructures); ++i){
            //get command and first patch
            c = &commandstructures[i];
            p = &patchstructures[0];
            int count = 0;
            
            //match patch name
            while(strcmp(c->patchname, p->name) != 0){
                 printf("%s %s",c->patchname, p->name);
                 count++;
                 p = &patchstructures[count];                           
            }
            
            //put the patch on the frame in the correct location
            int x, y; 
            for(x = 0; x < p->height; ++x){
                for(y = 0; y < p->width; ++y){
                    myframe[c->xcoor][c->ycoor] = p->patch[x][y];      
                }         
            }           
        }
    Ive got my debug code set up there, and when I run the files the sample output shows a match, but it continues through the loop until the program crashes. Here is the output:

    Code:
    File List:
         patches1.txt
         patches2.txt
         patches3.txt
         commands1.txt
         commands2.txt
         commands3.txt
    
    
    
    Input a patches filename:  patches1.txt
    
    Input a commands filename:  commands1.txt
    rect rect
    rect ex
    rect dollar
    rect rect rect rect rect rect rect rect rect EL32.dllrect
    Got me all shook up. I'm tired and I want to get this done. Ha...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are both these strings terminated with the end of string char? ('\0')

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    15
    Not sure, although I do remember now that strcmp requires this, no? How can I figure out if they do?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I usually specify it in the code that puts the char's into the variable. Just append the end of string char, right off. Then no more worries.

    You could check it by having it print out the string, or print out the strlen(string) for you. If it's too long, then you know it's run over because it didn't find the end of string char.

    Letters and numbers intended for a string, are just letters and numbers, AND NOT STRINGS, until they have an end of string char placed after them.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    15
    It did not find the \0. Here is the code where I read in the strings:

    Code:
        int count = 0;
    
        //loop to get the coordinats and name of the patch
        while(fgets(line, sizeof(line), commands) != NULL) {
    
            sscanf(line,"&#37;d %d %s", &c->xcoor, &c->ycoor, c->patchname);
                    
            //add to structure array
            commandstructures[count] = *c;
            count++;                  
        }   
    
        ........
    
         char line[100];
         int count = 0;
         
         while(fgets(line, sizeof(line), patches) != NULL) {
             //name
             strcpy(p->name, line);
          
         .........
    c->patchname is char patchname[10] inside my structure. maybe someone can offer up something based on this...
    Last edited by krsauls; 05-02-2007 at 03:47 AM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    fgets puts the \n symbol in the buffer...
    if you want to copy its contents to some var - you should remove this char from the buffer before copying...

    You can look at the FAQ to see some samples illustrating it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    15
    Thanks...I will try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  3. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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