Home > Back-end >  Calling one function inside another, does not execute as desired
Calling one function inside another, does not execute as desired

Time:11-09

Since I've had several members tell me to post the whole program I'm gonna post the whole program so you can exectue it.

In this program I want to be able to register car parts and change the inventory balance.

Now to the issue. Every function itself worsk well, the problem starts when I call searchIt() function to changeIn() function. I need searchIt() so I can search the item before modifying its inventory balance.

Issues:

  1. Whenever I serach for an item and change inventory on that item, it changes on every item.

  2. In menu if I choose (3)change inventory balance and then serach for an item that does not exist it does not tell me "Wrong item number" instead it exits the program.

  3. The inventory balance goes to negative numbers.

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

#define MAXELENGTH 20
#define MAX 100

struct car{

    int itemNmr;
    char name[MAXELENGTH];
    int inventory;
};
typedef struct car Car;

void registerArticle(Car a[], int *pN);
void print(Car a[], int n);
void changeIn(Car a[], int n);
int searchIt(Car a[], int n);


Car createIt(int itemNmr, char name[],int inventory){

    Car c;
    c.itemNmr = itemNmr;
    strcpy(c.name, name);
    c.inventory = inventory;
    return c;
}

int main(){


    Car reg[MAX]; 
    int choice;
    int nrOfIt=0; 

    while(1){
        printf("(1)Register new pars\n(2)Display all parts\n(3)Change inventory\n(4)Search\n(5)Exit\n");
        scanf("%d", &choice);

        switch(choice){
                case 1: registerArticle(reg, &nrOfIt);
                        break;
                case 2: print(reg,nrOfIt);
                        break;
                case 3: changeIn(reg,nrOfIt);
                        break;
                case 4: searchIt(reg,nrOfIt);
                        break;
                case 5: printf("Exit");
                        return 0;
                default: printf("Try again!");
                        break;
        }
    } return 0;
}

void registerArticle(Car a[], int *pN){

    int inventory;
    int itemNmr;
    char name[MAXELENGTH]; 

    while(1){

        printf("Item number(0 to exit): ");
        scanf("%d%*c", &itemNmr);
        
        if(itemNmr==0){
            return;
        }
        printf("Name: ");
        scanf("%s%*c", name);
        printf("Inventory: ");
        scanf("%d%*c", &inventory);
        a[*pN]=createIt(itemNmr,name,inventory);
        (*pN)  ;   
    }
}

void print(Car a[], int n){

    if(n==0){
        printf("the list is empty\n");
    }else{
       for(int i=0;i<n;i  ){
         printf("%d\t\t%s\t\t%d\n", a[i].itemNmr, a[i].name, a[i].inventory);
       }
       return;
    }
}

int searchIt(Car a[], int n){
    
    while(1){

        int itemN;          
        printf("Type item number: ");
        scanf("%d", &itemN);
        if(itemN==0){
            break;
        }
        int found =0;
        for(int i=0;i<n;  i)
        {
          if(itemN==a[i].itemNmr)
          {                                           
              printf("%d\t\t%s\t\t%d\n", a[i].itemNmr, a[i].name, a[i].inventory);
              return i;                         
              break;                   
          }                   
       }
         if(!found)
         {
         printf("Wrong item number!");
         }
}
    return 0;
}

void changeIn(Car a[], int n){

        int input;
        int i;
        searchIt(a,i);
     
        printf("Increase or decrease by: ");
        scanf("%d", &input);
 
        for(i=0;i<n;i  )
            a[i].inventory  = input;

        if(a[i].inventory<0)
            a[i].inventory = 0;       
}
       ```

CodePudding user response:

This:

void changeIn(Car a[], int n)
{
  int input;
  int i;
  searchIt(a, i);

passes an uninitialized register-length to searchIt(), probably causing it to go way out of bounds. Then it throws away the return value of searchIt(), and then uses the still uninitialized i to index into the array. Not good. It should be:

void changeIn(Car a[], int n)
{
  int input;
  const int i = searchIt(a, n);

Edit:

As you pointed out in a comment, yes the loop in changeIt() makes no sense; you don't want to change more than one element so there is no need to loop. Looping is for expressing repetition and there's no need for that here.

It should just be:

    a[i].inventory  = input;
    if (a[i].inventory < 0)
        a[i].inventory = 0;
  •  Tags:  
  • c
  • Related