Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Help with a menu driven linked list program?

+1 vote
asked May 2, 2019 by Morwyn (130 points)
1.    Write a menu driven inventory maintenance program using a linked list.  Each node of the list should contain three data fields: the item name, the item number and the number of units in stock.  Initially the list should be empty.  A menu should be developed which allows the programmer to perform the following operations:

a.    G – Get the existing data from a binary file (created above)
b.    I -- Insert a new item in the list – user must enter item name, item number and number of units.
c.    D -- Delete an existing item – user must enter an item number only to do this
d.    R – Reduce the number of units for a particular item – user must enter item number and number of units removed.
e.    P -- Print all of the items in stock and the number of units of each.
f.    Write a new list back to the file.
g.    U – Update the original file with new information


Here is what I have so far:
#include<iostream>
#include<fstream>
using namespace std;

typedef struct node
{
    int data;
    node *next;
}node;

node *insertEnd(node *head, int val);
void getData(node *current_head)
void insertPos(node *current_head)
void delete_spe(node *&head, char *number, int &count)
void reduceInv(node *current_head)
void printList(node *current_head)
void updateData(node *current_head)
void showMenu();

int main()
{
    node *head = NULL;
    int input, val;

    ifstream myfile;
    myfile.open("input.txt");
    while (myfile >> val)
        head = insertEnd(head, val);


    while (true) {
        showMenu();
        cin >> input;
        switch (input) {
        case 1:
            getData (head);
            break;
        case 2:
            insertPos(head);
            break;
        case 3:
            delete_spe (head, num, count);
            break;
        case 4: reduceInv (head);
            break;
        case 5: printList (head);
            break;
        case 6: updateData (head);
            break;
        case 7: return 0;
            break;
        default: cout << endl;
        }
        cout << endl;
    }
    return 0;
}


node *insertEnd(node *current_head, int val)
{
    node *temp;
    temp = current_head;

    if (temp == NULL) {
        node* newnode = new node;
        newnode->data = val;
        newnode->next = current_head;
        return newnode;
    }

    while (temp->next != NULL)
        temp = temp->next;

    node* newnode = new node;
    newnode->data = val;
    newnode->next = NULL;
    temp->next = newnode;
    return current_head;
}

void showMenu() {
    cout << "1. Get the existing data.\n";
    cout << "2. Insert a new item in the list .\n";
    cout << "3. Delete an existing item .\n";
    cout << "4. Reduce the number of units for a particular item .\n";
    cout << "5. Print list.\n";
    cout << "6. Update File\n";
    cout << "7. Quit\n";
    cout << "\n Please enter a choice: ";
}

//get data
void getData(node *current_head)
{
    string name;
    ofstream myfile;
    myfile.open("example.txt");
    while (!myfile.fail() && !myfile.eof())
    {
        myfile >> name;
        cout << name << endl;
    }
    myfile.close();
}
//insert new data
void insertPos(node *current_head)
{
    int position;
    cout << "Please enter the Name: ";
    cin >> Data.itemName;
    cout << "Please enter the Item Number: ";
    cin >> Data.itemNumber;
    cout << "Please enter the quantity: ";
    cin >> Data.numOfItems;
    cout << "Please enter the number after which you want to insert: ";
    cin >> position;
    node *temp = current_head;
    while (temp != nullptr && temp->data != position)
        temp = temp->next;
    //cout << temp->data << endl;  
    if (temp != nullptr)
    {
        node* newnode = new node;
        newnode->data = Data.itemName;
        newnode->data = Data.itemNumber;
        newnode->data = Data.numOfItems;
        newnode->next = temp->next;
        temp->next = newnode;
        cout << "Number inserted into the list." << endl;
    }
    else
        cout << "Element not found in the list." << endl;
}

//delete an item - edit to delete a number specified item
void delete_spe(node *&head, char *number, int &count)
{
    node* current, *temp;
    temp = head;
    current = head;

    if (head == NULL) //if head null
    {
        cout << "\n No element in list";
        return;
    }
    else
    {
        while (temp != NULL) //travesre till you get name in list
        {
            /*   if(temp->number==number)
            break;
            */
            //compare name and temp name
            if (strcmp(temp->name, number) == 0)
                break; //if found come out of the loop
            current = temp;
            temp = temp->next;
        }
        current->next = temp->next; //link current next node to temp next node
        delete temp; //delete temp
        count--;
    }
}

//reduce the number of units
void reduceInv(node *current_head)
{
    int position, reduction, newTotal;
    cout << "Please enter the item number: ";
    cin >> position;
    node *temp = current_head;
    while (temp != nullptr && temp->data != position)
    if (temp != nullptr)
    {
        cout << "How much would you like to reduce the inventory? \n";
        cin >> reduction;
        newTotal = item.Data.itemNumber - reduction;
        item.Data.itemNumber = newTotal;
        cout << "Inventory Updated" << endl;
    }
    else
        cout << "Element not found in the list." << endl;
}

//print
void printList(node *current_head)
{
    cout << "Linked List: ";
    while (current_head != nullptr)
    {
        cout << current_head->data << " ";
        current_head = current_head->next;
    }
    cout << endl;
}

//update the original file
void updateData(node *current_head)
{
    string name;
    ofstream myfile;
    myfile.open("example.txt");
    while (!myfile.fail() && !myfile.eof())
    {
        while (true) {
            showMenu();
            cin >> input;
            switch (input) {
            case 1:
                insertPos(head);
                break;
            case 2:
                deleteLast(head);
                break;
            case 3:
                frequency(head);
                break;
            case 4: sort(head);
                break;
            case 5: printList(head);
                break;
            case 6: return 0;
                break;
            default: cout << endl;
            }
            cout << endl;
    }
    myfile.close();
}

//

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...