0

First of all sorry for my English. Second I'm new in c and data structures.

I created a menu and put it in loop so that I could use it to make operations like insert, remove(all the tree nods), remove(one node), create(from a file) and so on. All the methods were working except the insert method. I can't see why because there is no compilation error. It insert only the first and last element on the tree. And if the strings have different sizes it stops running.

It ends the loop when I open the file and the loop in my menu.

The file has number starting from 0 to 9 and letters starting from a to z, and next to them it has a code, like this:

0 -----
1 .----
.
.
a ..-.-
b ..-..
.
z .-

Here is the code:

struct treeB{
    struct treeB *left; 
    struct treeB *right;
    char letter[2];
    char code[5];
}*root=NULL;


typedef struct treeB tree;

void insert(tree* aux){

//I create the tree and allocate the memory in the main

int flag=0;
tree *ptr, *father;
ptr=root;

if(root==NULL){
    root=aux;
}else{
    while(ptr != NULL){
        if(strcmp(aux->code, ptr->code)==1){
            father=ptr;
            ptr=ptr->right;
        }else{
            if(strcmp(aux->code, ptr->code)==-1){
                father=ptr;
                ptr=ptr->left;
            }else{
                flag=1;
                printf("\n The code %s already exists");
                break;
            }
        }
        
        if(flag==0){
            if(strcmp(father->code, aux->code)==1){
                father->left=aux;
            }else{
                if(strcmp(father->code, aux->code)==-1){
                    father->right=aux;
                }
            }
        }
    }   
}   
}
  • 2
    Please post a [mre]. – wohlstad Jul 06 '22 at 07:34
  • 2
    If the error happens in `insert` you should not hide that function from us. – Gerhardh Jul 06 '22 at 07:35
  • Did you try to run your program in a debugger and walk through your loop? Did you inspect the values of all variables before the error happens? If not, that is a good point to start with. – Gerhardh Jul 06 '22 at 07:36
  • General note: You should not only check return value of `fscanf` for `EOF` but for the expected value. Also you should always include a length limit for reading strings. Otherwise you could get buffer overflows. – Gerhardh Jul 06 '22 at 07:38
  • What is that format string supposed to do `"%s %s[^\n]\n"` ? You read 2 strings (with missing length limit) and then your input must match `[^\n]\n`. Did you mean `"%1s %4[^\n]\n"`? `[^\n]` is not a modifier for `%s` but a different format specifier. – Gerhardh Jul 06 '22 at 07:40
  • One more hint: When you edit your question to include the requested MRE, please also add sample content of your input file. At least add the first few lines. If your error only happens later at a certain line, also include that line and a few lines before. – Gerhardh Jul 06 '22 at 07:55
  • 2
    [Mandatory reading](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong). – n. m. could be an AI Jul 06 '22 at 11:38
  • How many characters are in `..-.-`? How big is `struct treeB`'s `code` field? – Andrew Henle Jul 06 '22 at 12:08
  • Sorry, you where right the problem is on insert it just get first and last elements inserted and if the strings have a diferent length it does'nt run anymore. – Eric Mascarenhas Jul 06 '22 at 18:10

0 Answers0