Godpoong
Godpoong

Reputation: 25

Problem with function and while loop in C: could not find reason why I cannot exit the loop

I have a code like below:(It is working well)

#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            // divide the given polynomial expression with "()"
        func = malloc(sizeof(int));
        scanf("%[^\n]s", func);
        char *cal[20] = { NULL, };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        return 0;
}

And I got result (When I input (3x**2+4x**1+4)*(2x**2+3))

(3x**2+4x**1+4)
*
(2x**2+3)
Complete 1

So I tried to do more types of manipulating with adding more while loop under the code above:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            // divide the given polynomial expression with "()"
        func = malloc(sizeof(int));
        scanf("%[^\n]s", func);
        char *cal[20] = { NULL, };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        int j = 0;           // Change + to +- in the divided expression
        char *num[20] = {NULL, };
        char ptr2 = strtok(cal, "+");
        while (2j - 2 < i){
                for(int k = 0; k< strlen(cal[2j-2]); k++){
                        if ((cal[2j-2] + k) == '-'){
                                char s[] = "+";
                                memmove(cal[2j - 2] + k-1, s, strlen(s));
                        }
                }

        }
        printf("Complete 2");
        return 0;
}

However, in this case, I got a result like

(3x**2+4x**1+4)
*
(2x**2+3)

and the function is not over.

I think I stuck in the first while loop because 'Complete 1' is not printed out.

Why this situation happens?

Thank you for reading and any help would be greatly appreciated.

P.S. I ran this code on linux environment with gcc command.

Upvotes: 0

Views: 46

Answers (1)

Your code has several major problems which caused a lot of warnings and even errors as I tried to compile it, as you can see here.

Apparently, you ignored or disabled compiler warnings, which seems to be the root for the issue. Here is a link for more information:

Why should I always enable compiler warnings?


I do not understand the logic behind your algorithm so I can´t give you a reliable answer, so this shall be more treated as an extended comment than an answer just for sake of bringing you further.

Nonetheless, here is the code which at least fixes all of the syntactic issues. I can´t explain them all in detail because they are just too much. Compare the code with yours and you´ll see:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

int main(){
        char* func;            
        func = malloc(sizeof(*func) * 8);
        scanf("%[^\n]", func);
        char *cal[20] = { NULL };
        int i = 0;
        char *ptr = strtok(func, "()");
        while (ptr != NULL){
                cal[i] = ptr;
                printf("%s\n", cal[i]);
                i++;
                ptr = strtok(NULL, "()");
        }
        printf("Complete 1");
        int j = 0;           
        char *num[20] = { NULL };
        char* ptr2 = strtok(*cal, "+");
        char s[2] = "+";
        while (j - 2 < i){
                for(int k = 0; k < strlen(cal[j-2]); k++){
                        if ((*(cal[j-2] + k)) == '-'){

                             memmove(cal[j - 2] + (k-1), s, strlen(s));
                        }
                }
        }
        printf("Complete 2");
        return 0;
}

No warranty that this will work in the way desired. It should only give you a clue in the right direction.

Upvotes: 1

Related Questions