Home > Software engineering >  Modifying variables inside a struct in C
Modifying variables inside a struct in C

Time:11-22

I'm learning C right now and am having trouble with a program I'm writing. Essentially, the program simulates a parts inventory with part names and quantities. I need to have a function to add parts to the bin (addParts), and a similar function to remove them, but I have no idea how to modify the variables in the struct. How do I actually access the variables in a function outside of the main and update the variable values? I've tried watching several tutorials and it didn't help because I'm panicking over the fact that this is due tonight and my brain is turning to mush. I don't expect it to be written for me, but some guidance would be really helpful. EDIT: I realized I needed to clarify that the program is supposed to allow the user to add or subtracts parts QUANTITY, but not to add or remove a part type or category.

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

int prt;
int num;
int count;

void addParts()
{
    while(1)
    {
        printf("\nType the number of the part you wish to add. ");
        scanf("%d", &prt);

        printf("\nHow many parts would you like to add? ");
        scanf("%d", &num);
    }
}
void removeParts()
{

}
int main()
{
    struct Inventory
    {
        char name[15];
        int num;
        int qty;
    };
    struct Inventory part1, part2, part3, part4, part5;
    struct Inventory part6, part7, part8, part9, part10;
    strcpy(part1.name, "Valve");
    part1.num = 1;
    part1.qty = 10;
    strcpy(part2.name, "Bearing");
    part2.num = 2;
    part2.qty = 5;
    strcpy(part3.name, "Bushing");
    part3.num = 3;
    part3.qty = 15;
    strcpy(part4.name, "Coupling");
    part4.num = 4;
    part4.qty = 21;
    strcpy(part5.name, "Flange");
    part5.num = 5;
    part5.qty = 7;
    strcpy(part6.name, "Gear");
    part6.num = 6;
    part6.qty = 5;
    strcpy(part7.name, "Gear Housing");
    part7.num = 7;
    part7.qty = 5;
    strcpy(part8.name, "Vacuum Gripper");
    part8.num = 8;
    part8.qty = 25;
    strcpy(part9.name, "Cable");
    part9.num = 9;
    part9.qty = 18;
    strcpy(part10.name, "Rod");
    part10.num = 10;
    part10.qty = 12;

    while (1)
    {
        int response;
        printf("-------------------------\n"
            "        INVENTORY\n"
            "PART                 QTY\n"
            "-------------------------\n");
        printf("1.  %s           | %d \n", part1.name, part1.qty);
        printf("2.  %s         | %d \n", part2.name, part2.qty);
        printf("3.  %s         | %d \n", part3.name, part3.qty);
        printf("4.  %s        | %d \n", part4.name, part4.qty);
        printf("5.  %s          | %d \n", part5.name, part5.qty);
        printf("6.  %s            | %d \n", part6.name, part6.qty);
        printf("7.  %s    | %d \n", part7.name, part7.qty);
        printf("8.  %s  | %d \n", part8.name, part8.qty);
        printf("9.  %s           | %d \n", part9.name, part9.qty);
        printf("10. %s             | %d \n", part10.name, part10.qty);
        printf("-------------------------\n");

        printf("Would you like to 1-Add Parts, 2-Remove Parts, "
            "or 3-Quit? ");
        scanf("%d", &response);
        switch(response)
        {
            case 1:
                addParts();
            case 2:
                removeParts();
            case 3:
                break;
            case '\n':
                break;
            default:
                printf("\nINVALID INPUT\n\n");
                getchar(); //clears out input
                break;
        }
        if (response == 3)
        {
            break;
        }
    }
    return 0;
}

CodePudding user response:

You need to put your inventory in an array. Because you want to add or remove products it should be dynamically allocated one.

One of the functions you need:

struct Inventory
{
    char name[15];
    int num;
    int qty;
};

struct Database
{
    size_t nParts;
    struct Inventory Parts[];
};

struct Database *addPart(struct Database *database, const char *name, int num, int qty)
{
    size_t newsize = database ? database -> nParts   1 : 1;

    database = realloc(database, newsize * sizeof(database -> Parts[0])   sizeof(*database));
    if(database)
    {
        database -> nParts = newsize;
        strcpy(database -> Parts[newsize - 1].name, name);
        database -> Parts[newsize - 1].num = num;
        database -> Parts[newsize - 1].qty = qty;
    }
    return database;
}

CodePudding user response:

If you want to add new inventory types, then a dynamic array is a really good option. If not, then a static array would be very much simpler.

static struct {
    const char name[15];
    int qty;
} database[] = {
    { "Valve", 10 },
    { "Bearing", 5 },
    { "Bushing", 15 },
    { "Coupling", 21 },
    { "Flange", 7 }
    /* etc */
};
static const size_t database_size = sizeof database / sizeof *database;
static const int name_len = sizeof database->name - 1;

I see the part number is intended to be used as a primary key, but it's not displayed anywhere, so you are supposed to guess based on the ordinal? I did away with that, instead relying on the computed value in the array. As a sequential monotonic value, this is guaranteed to be unique. When we enter part number, it's number - 1 in the array.

for(size_t i = 0; i < database_size; i  )
    printf("%3zu. %-*s| %d\n",
    i   1, name_len, database[i].name, database[i].qty);

Then you can automate the printing. If database is above the functions addParts and removeParts, then you can refer to it. This is a case where singleton design is appropriate, I think.

  • Related