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

using c statements, write a program that should ask the user with the following

ID: 3860572 • Letter: U

Question

using c statements,

write a program that should ask the user with the following menu options:

1: Add a number of items into inventory.
2: Print current inventory contents.
3: Save current inventory contents to a file

4: Exit program.

An Item should be represented by a user-defined data type (struct). An Item has the following fields:

Item id

Item name

Item price

Item quantity

- If option 1 is selected:

your program should prompt the user for:

1.how many items they wish to enter, and this number should be used to allocate memory for a dynamic array of Items

2. prompt the user to input the three fields associated with each Item , and store the Items inside of the array.

-If option 2 is selected:

your program should:

1.print the contents of the array of Items that your program is internally maintaining, in a visibly pleasing way, to standard output.

- If option 3 is selected:

your program should:

1. print the contents of the array of Items that your program is internally maintaining, in a visibly pleasing way, to a file called inventory.txt .

- If option 4 is selected

your program should:

1. gracefully exit with a farewell message.

Explanation / Answer

NOTE: I have written the code for your assignment. Please check and let me know if you have any questions. I will revert back within 24 hours.

Code:
#include <stdio.h>
#include <stdlib.h>

struct inventory{
    int item_id;
    char item_name[30];
    float item_price;
    int item_quantity;
};

int main()
{
    int choice = 0, no_of_items = 0, i;
    struct inventory *ptr;
    FILE *fptr;

    while(choice != 4){
        printf(" =========================");
            printf(" Inventory Menu Calculator");
            printf(" =========================");
        printf(" 1. Add a number of items into inventory");
        printf(" 2. Print current inventory contents");
        printf(" 3. Save current inventory contents to a file");
        printf(" 4. Exit program");
        printf(" Enter your chocie ? (1/2/3/4) ");
        scanf("%d", &choice);
        if(choice == 1){
            printf(" How many items you wish to enter? ");
            scanf("%d", &no_of_items);
            ptr = (struct inventory *) malloc(no_of_items * sizeof(struct inventory));
            for(i = 0; i < no_of_items; i++){
                printf(" Enter item id: ");
                scanf("%d", &(ptr + i)->item_id);
                printf("Enter item name: ");
                scanf("%s", (ptr + i)->item_name);
                printf("Enter item price: ");
                scanf("%f", &(ptr + i)->item_price);
                printf("Enter item quantity: ");
                scanf("%d", &(ptr + i)->item_quantity);
            }
        }
        else if(choice == 2){
            if(no_of_items > 0){
                printf(" Item Id, Item Name, Item Price, Item Quantity");
                for(i = 0; i < no_of_items; i++)
                    printf(" %d, %s, %f, %d", (ptr + i)->item_id, (ptr + i)->item_name, (ptr + i)->item_price, (ptr + i)->item_quantity);
            }
            else{
                printf(" Inventory does not have any items. Please choose option 1 to add items to the inventory...");
            }         
            printf(" ");
        }
        else if(choice == 3){
            fptr = fopen("inventory.txt", "w");
            if(fptr == NULL){
                printf(" Some error while opening the file for writing.");
                exit(1);
            }
            printf(" Writing inventory contents to inventory.txt file.... ");
            for(i = 0; i < no_of_items; i++)
                fprintf(fptr, " %d, %s, %f, %d", (ptr + i)->item_id, (ptr + i)->item_name, (ptr + i)->item_price, (ptr + i)->item_quantity);
            fclose(fptr);
        }
        else if(choice == 4){
            printf(" Happy Inventory operations. Exiting... ");
            break;
        }
        else{
            printf(" Invalid choice. Options available are 1/2/3/4. Please try again... ");
        }
    }

    return 0;
}

Code output screenshot:
https://pasteboard.co/GCEk178.png
https://pasteboard.co/GCEkbgb.png