On-screen Print Error

Asked

Viewed 30 times

0

Good afternoon,

I’m developing a console application for a university paper. The goal is to create a stock management application of a pharmacy using C language.

I created the function of adding new medicines (AddNewDrug) and to show all items in the list (ShowUpOrderedList). However, the information is not being printed correctly.

I know that the error is probably in the function of adding since the function that prints the information prints everything that is text, just does not print the variables.

This is output on the console:

Output do programa


This is my code:

Add function

void AddNewDrug(Drug **head, Drug **tail, int registryId, int drugId, char *designation, char *labName, int unitsInStock)
{
    // variables
    Drug *drugToAdd;

    // prepare drug to be added
    drugToAdd = (Drug*) malloc(sizeof(Drug));
    drugToAdd->registryId = registryId;
    drugToAdd->drugId = drugId;
    drugToAdd->unitsInStock = unitsInStock;
    strcpy(drugToAdd->designation, designation);
    strcpy(drugToAdd->labName, labName);

    // if the list is empty, add the drug on the first position (head and tail simulteanously)
    if(*head == NULL)
    {
        drugToAdd->next = NULL;
        drugToAdd->previous = NULL;
        *head = drugToAdd;
        *tail = drugToAdd;
    }
    else // if the list is not empty, compares the drug code to find if it is lower or higher and adds the drug
    {
        // local variables
        Drug *actual; // used to compare drug code and verify place of insertion

        // local variables initialization
        actual = head;

        // comparing drug code before adding drug. stops when finding a higher code or when finding a null element
        while(actual != NULL)
        {
            if(drugToAdd->drugId < actual->drugId)
            {
                actual = actual->next;
            }

            actual = actual->next;
        }

        // if the actual is the head, change the head
        if(actual == head)
        {
            drugToAdd->next = actual;
            drugToAdd->previous = NULL;
            actual->previous = drugToAdd;
            *head = drugToAdd;
        }
        else if(actual == NULL) // if the actual is after the tail, change the tail
        {
            drugToAdd->next = NULL;
            drugToAdd->previous = *tail;
            (*tail)->next = drugToAdd;
            *tail = drugToAdd;
        }
        else // actual is in the middle of the list
        {
        // adding the drug
        drugToAdd->next = actual;
        drugToAdd->previous = actual->previous;
        actual->previous->next = drugToAdd;
        actual->previous = drugToAdd;
        }
    }
}
  • As, it seems to me, the problem is in your printing function you missed posting such function.

  • Thank you for the comment, but the problem has already been solved. It was found in the add function and related to misreferenced pointers and wrong stop conditions.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.