Like Tree2Likes
  • 2 Post By AndiPersti

strcmp question

This is a discussion on strcmp question within the C Programming forums, part of the General Programming Boards category; Hi I have a candidate[MAX][100] i must compare if an element start by "M" i put like this but not ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    strcmp question

    Hi

    I have a candidate[MAX][100]

    i must compare if an element start by "M"

    i put like this but not working like it should.

    Code:
    for(i=0;i<MAX;i++
    {
     if(strcmp((char*)candidatos[i],"M")==0)
    {
    
    }
    }
    Any help please?

    Thanks

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    First, don't cast to char*. If you added a cast to silence a warning, that is wrong. Rarely is casting the right way to fix a compiler warning.

    Now, you need to provide more information. You forgot to give the type of "candidatos". You say it is "not working like it should", but you fail to explain how it is not working.

    Here's a tip: provide a complete program (one that compiles), that shows the problem. Don't just half-assedly type in a part of your source code. Who knows what errors you've introduced by typing it in. At the very least, you're missing a ) character.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    63
    Quote Originally Posted by alphasil View Post
    Hi

    I have a candidate[MAX][100]

    i must compare if an element start by "M"

    i put like this but not working like it should.

    Code:
    for(i=0;i<MAX;i++
    {
     if(strcmp((char*)candidatos[i],"M")==0)
    {
    
    }
    }
    Any help please?

    Thanks
    First read How To Ask Questions The Smart Way. Also, most people just skip code that does not compile unless the question is "why does not my code compile", and even then your answer will probably be "Because you wrote X lines of code without compiling and testing".

    So read the link that I posted and then come back with code that compiles and enough of that so you can actually answer any questions about it. Also state what you have tried to fix the problem yourself.

    You'll definitely learn a lot more if you put some effort into it.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    OK,

    So the type is

    Code:
    #define MAX 5
    char candidatos[MAX][100]
    The function is

    Code:
    int escreveCandidatosTXT(char candidatos[][100])
    {
        FILE *f;
        int i;
        f=fopen("candidatosM.txt", "a+");
        if(!f)
        {
            printf("Erro no ficheiro");
            return -1;
        }
        for(i=0; i<MAX; i++)
        {
            if(candidatos[i][0]=='M')
            {
                fprintf(f, "%s",(char*)candidatos[i]);
            }
            else
            {
                printf("Nothing to write...");
            return 0;
            }
        }
        return 0;
    
    
    }
    The file must be writen if the names start by "M"

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    63
    This is still not a question. Did you read the link that I gave you?

    Come back again with a question, preferably a question that shows that you have made an effort to solve this yourself.

    I mean, what happens when you try to compile your program? What happens when you run it, if it compiles? What do you expect to happen?

    If you want to ask for help and have a function that does not work then you should at least include the code for the function and any code it needs to run. You should also include code showing how you call that function.

    A good way to learn programming is to start with a small program that compiles. Add a few lines of code to that program. Fix it so that it compiles cleanly without warnings. Verify that it does what you intend it to do. Then add some more lines of code and rinse and repeat.

    I don't mean to be mean. You will learn more and faster this way.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    if(candidatos[i][0]=='M')
    {
        fprintf(f, "%s",(char*)candidatos[i]);
    }
    else
    {
        printf("Nothing to write...");
        return 0;
    }
    I highly recommend using a debugger and stepping through the program. What happens when you come across a name which doesn't start with a 'M'?

    Bye, Andreas
    Salem and Jimmy like this.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Quote Originally Posted by Jimmy View Post
    I mean, what happens when you try to compile your program? What happens when you run it, if it compiles? What do you expect to happen?
    The program compiles, no errors at all

    I can write names into the array, i can sort the array, i can print the array, and then i must write into a txt file, the names starting by M.

    This is the full code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 3
    
    
    int ordenaCrescente(char candidatos[][100])
    {
        int i,j;
        char aux[100];
        for(i=0; i<MAX-1; i++)
        {
            for(j=MAX-1;j>i; j--)
            {
                if(strcmp((char*)candidatos[j],(char*)candidatos[j-1])<0)
                {
                    strcpy(aux,(char*)candidatos[j]);
                    strcpy((char*)candidatos[j],(char*)candidatos[j-1]);
                    strcpy((char*)candidatos[j-1], aux);
                }
            }
        }
        return 0;
    }
    
    
    int escreveCandidatosTXT(char candidatos[][100])
    {
        FILE *f;
        int i;
        f=fopen("candidatosM.txt", "a+");
        if(!f)
        {
            printf("Erro no ficheiro");
            return -1;
        }
        for(i=0; i<MAX; i++)
        {
            if(candidatos[i][0]=='M')
            {
                fprintf(f, "%s",(char*)candidatos[i]);
            }
            else
            {
                printf("Nothing to write...");
            return 0;
            }
        }
        return 0;
    
    
    }
    
    
    int listar(char candidatos [][100])
    {
        int i;
        for(i=0; i<MAX; i++)
        {
            printf("%s",(char*)candidatos[i]);
        }
        return 0;
    }
    
    
    int main()
    {
        char candidatos[MAX][100];
        int i;
        for(i=0; i<MAX;i++)
        {
            printf("Nome: \n");
            fgets((char*)candidatos[i], 100, stdin);
        }
        printf("List: \n");
        listar(candidatos);
        printf("Sorted list: \n");
        ordenaCrescente(candidatos);
        for(i=0; i<MAX;i++)
        {
             printf("%s",(char*)candidatos[i]);
        }
        escreveCandidatosTXT(candidatos);
        system("pause");
        return 0;
    }
    So, the program runs well, the problem is if i have a name started by M he doesn't write anything into my txt file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp question
    By lowestOne in forum C Programming
    Replies: 32
    Last Post: 06-09-2012, 04:00 PM
  2. strcmp question
    By voidpain() in forum C Programming
    Replies: 5
    Last Post: 08-11-2011, 09:16 PM
  3. question about strcmp
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 03-07-2009, 09:39 PM
  4. Strcmp Question - C
    By Dr.Zoidburg in forum C Programming
    Replies: 8
    Last Post: 01-22-2009, 09:03 AM
  5. Question about strcmp
    By readerwhiz in forum C Programming
    Replies: 1
    Last Post: 09-23-2001, 05:18 PM

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