vendredi 12 mars 2021

C - How to print this line my if-statement doesn't work in C? [closed]

I'm having some trouble to output to console this "method" variable. Can somebody help me out?

while (word != NULL) 
        {
            char method [50];
            int print_method = 0;
            
            if (word != NULL && strcmp(word, "public") == 0) 
            {
                char* jmp = strtok(NULL, delimeters);

                if (jmp != NULL && strcmp(jmp, "class") == 0) 
                {
                    char* class_name = strtok(NULL, delimeters);
                    printf("Class: %s\n", class_name);
                    fprintf (output,"%s\n", line_copy);
                } 
                else 
                {
                    char* method_name = strtok(NULL, delimeters);
                    
                    
                    print_method = 1;
                    method[0] = '\0';
                    strcat(method, method_name);
                    printf("Method: %s\n", method);
                    fprintf (output,"%s\n", line_copy);
                }
            }
            
            if(print_method = 1)
            {
            printf("Method: %s\n", method);
            method[0] = '\0';
            }
            
            word = strtok(NULL, delimeters);
        }

If I use printf("Method: %s\n", method); right down beneath strcat it prints as it should be: **

Method: getMatric
Method: setMatric
Method: getName
Method: setName
Method: getAddress
Method: setAddress
Method: toString

**

But the other one inside the if(print_method = 1) doesn't, and this is the one that I want to work. It prints: **

Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: getMatric
Method: getMatric
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: setMatric
Method: setMatric
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: getName
Method: getName
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: setName
Method: setName
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: getAddress
Method: getAddress
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: setAddress
Method: setAddress
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method: toString
Method: toString
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:
Method:

**

Aucun commentaire:

Enregistrer un commentaire