Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Program /*Please use comments to explain the code*/ 1 Write a C program that w

ID: 3871629 • Letter: C

Question

C Program /*Please use comments to explain the code*/

1 Write a C program that will keep track of the number of goods in a warehouse for various goods. Each will be represented by a struct containing the item number, the name and availability. The program should via a menu provide the opportunity to perform the following actions as long as the user wants:

1)Register new products 2) Print All products 3) Search for products 4) Change stock balance of goods 5) Sort products 6) Unregister goods 7)Exit the program.

When the program is launched, the program will ask the user to enter a file and then the program will load the item number, name and balance from the file and save it in an array of struct. If the file is missing, the program will start with an empty array.

When the program ends, it will overwrite / create a text file (not binary) with the previously specified name and where to save the struct in the array so that they can be loaded the next time the program is run.

Register new products

Here can we register new goods.

Print all goods

When the user selects this option prints all items in the program in the order they are stored in the program and then the main menu comes up again.

Explanation / Answer

// Only first four parts of the question have been answered

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


// we shall have a maximum of 100 items in the warehouse, to increase just change the value of this
#define MAX_STOCK_ITEMS 100

#define MAX_STOCK_PER_ITEM 500 // maximum 500 number of each item are allowed

// define the structure for the warehouse
struct stock
{
    //
    char itemName[30];
    //
    char itemNumber[10];
    //
    char stockCount[4];
    //
    int filled; // this is a flag that indicates if a particular stock data is present or not
    //
}typedef stock;
//
void registerItem(stock stockData[]);
//
void printItems(stock stockData[]);
//
int searchItem(stock stockData[]);
//
int main()
{
    int choice;
    //
    char fname[20], fdata;
    //
    int i = 0, charCount_num = 0, charCount_name = 0, charCount_qty = 0, count = 0;
    //
    FILE *fp; // this is the file pointer
    //
    stock stockData[MAX_STOCK_ITEMS]; // an array of structure varaibles
    //
    printf("Enter source file : ");
    //
    scanf("%s", fname);
    //
    // if user does not specify a file name, start with a
    if (strlen(fname) == 0)
        memset(fname, 0, sizeof(fname));
    //
    // if a file name was provided, read it and populate the structure array declared above
    else
    {
        //
        fp = fopen(fname, "r"); // open the file in read mode
        //
        fdata = fgetc(fp); // read the first character
        //
        memset(stockData[i].itemNumber, 0, sizeof(stockData[i].itemNumber)); // clear the arrays before use
        //
        memset(stockData[i].itemName, 0, sizeof(stockData[i].itemName));
        //
        memset(stockData[i].stockCount, 0, sizeof(stockData[i].stockCount));
        //
        // loop and read till we read End Of File
        while (fdata != EOF)
        {
            // each line in the file contains info about one item, so each new line character means a new item
            if (fdata == ' ')
            {
                i++;
                //
                memset(stockData[i].itemNumber, 0, sizeof(stockData[i].itemNumber)); // clear the arrays before use
                //
                memset(stockData[i].itemName, 0, sizeof(stockData[i].itemName));
                //
                memset(stockData[i].stockCount, 0, sizeof(stockData[i].stockCount));
                //
                // re-initialize all indexers
                charCount_name = 0;
                //
                charCount_num = 0;
                //
                charCount_qty = 0;
                //
                count = 0;
            }


            // we shall use semicolons as delimiter for various data of a single item
            if (fdata == ';')
                count++;
            //
            // first item number, no ; till now
            if (count == 0)
            {
                // we dont want to store the ; and new lines
                if (fdata != ';' && fdata != ' ')
                {
                    stockData[i].itemNumber[charCount_num] = fdata;
                    ///
                    charCount_num++;

                }

            }
            //
            // the first ; separates the item name
            if (count == 1)
            {
                // we dont want the semicolons to be stored
                if (fdata != ';' && fdata != ' ')
                {
                    stockData[i].itemName[charCount_name] = fdata;
                //
                    charCount_name++;
                }

            }
            //
            // the second ; separates the item availability
            if (count == 2)
            {
                if (fdata != ';' && fdata != ' ')
                {
                    // a counter is used to index the characters in the array
                    stockData[i].stockCount[charCount_qty] = fdata;
                    //
                    // increment the counter
                    charCount_qty++;
                }

            }
            //
            stockData[i].filled = 1; // set the flag once the item data has been filled
            //
            fdata = fgetc(fp); // read the next character

        }
    }
    //
    //fp.close(); // close the file once the reading is done
    //
    // now ask the user for operations
    while(choice != 5)
    {
        //
        printf("Enter choice ");
        //
        printf("1 : Register an item 2: Print items 3: search an item, 4:change item quantity 5: Exit :");
        //
        scanf("%d", &choice);
        //
        switch(choice)
        {
            //
        case 1:
            registerItem(stockData); // pass the array and index
            //
            break;
        case 2:
            printItems(stockData);
            //
            break;
        case 3:
            i = searchItem(stockData);
            //
            if (i == 1)
                printf("Item not found ");

            break;
            //
        case 4:
            //changeStockQty(stockData);
            //
            break;
        case 5:
            return 0;
        }
    }

    return 0;
}
//
void registerItem(stock stockData[])
{
    //
    char number[10], name[30], qty[4];
    //
    int i = 0;
    //
    // clear all buffers before use
    memset(number, 0, sizeof(number));
    //
    memset(name, 0, sizeof(name));
    //
    memset(qty, 0, sizeof(qty));
    //
    // ask user for data
    printf("Enter item name : ");
    //
    scanf("%s", name);
    //
    printf("Enter item number: ");
    //
    scanf("%s", number);
    //
    printf("enter item quantity: ");
    //
    scanf("%s", qty);
    //
    // find an empty element in the stock array, filled cells have their filled flag set
    while(stockData[i].filled == 1)
        i++;


    // now i points to and empty cell
    strcpy(stockData[i].itemName, name);
    //
    strcpy(stockData[i].itemNumber, number);
    //
    strcpy(stockData[i].stockCount, qty);
    //
    printf("Stock added ");

}
//
// function prints all available stock
void printItems(stock stockData[])
{
    //
    int i = 0;
    //
    while(stockData[i].filled == 1)
    {
        //
        printf("%s;%s;%s ", stockData[i].itemNumber, stockData[i].itemName, stockData[i].stockCount);
        //
        i++;
    }

}
//
// function searches for an item using it's name
int searchItem(stock stockData[])
{
    //
    int i = 0;
    //
    char itemName[30];
    //
    printf("Enter item name to search ");
    //
    scanf("%s", itemName);
    //
    while(stockData[i].filled == 1)
    {
        //
        if (strcmp(stockData[i].itemName, itemName) == 0)
        {
            //
            printf("%s;%s;%s ", stockData[i].itemNumber, stockData[i].itemName, stockData[i].stockCount);
            //
            return 0;
        }
        //
        i++;
    }
    return 1;
}


// OUTPUT

NOTE : the testInputFile has not been included.

However, the data is :

123;shampoo;100
345;detergent;400
1225;juice;345

Paste it into a testInputFile.txt and keep the file in the same folder as the code.