Inventory Linke list. How....

I wrote this program that I used the linked list to do the inventory. But I wnat to know how do I put in the following transactions: 1.Taking-in- updating the Quantity and price of the item(if it is in the list) or add it to the list if it is not.
2. Retail- sel x items for 105% of the price.
3. Order-form- List all the inventory.
4. Quit.

the following is the code:         I am using Borland C++5.01

#include      <iostream.h>
#include      <string.h>

// Necessary in Turbo C++
//enum bool      {false, true};
// The structure for the node - separately
// defined because it is self-referencing

typedef      struct      {
      char      itemcode[6];
      char      description[20];
      int      quantity;
        float      price;
}      item;
struct      node      {
      item            data;
      struct      node      *next;
} node;

// Pointer type to the node
typedef      struct node      *pnode;

class      list      {
      list(item x);
      pnode      newnode(void);
      pnode      newnode(item x);
      void      addfront(item x);
      void      addrear(item x);
      void      insertafter(item x, pnode p);
      void      place(item x);
      bool      isxthere(char x[]);
      pnode      getptr(char x[]);
      void      removenode(char x[]);
      void      write(void);
      pnode      lstart;

// list() -      The default constructor - Starts the list as empty
      lstart = NULL;

// list() -      An initializing constructor that creates a node
//            and places in it the initial value
list::list(item x)
      lstart = new struct node;
      strcpy(lstart -> data.itemcode, x.itemcode);
      strcpy(lstart -> data.description, x.description);
      lstart -> data.quantity = x.quantity;
      lstart -> data.price = x.price;
      lstart -> next = NULL;

// ~list() -       The destructor
      pnode      p, q;

      // Go through the entire list, p points to the node because freed
      //      q points to the node after p
        if (lstart != NULL)
            for (p = lstart, q = p -> next;  q != NULL; p = q, q = p-> next)      
                  delete p;

    // When all is finished lstart is NULL
      lstart = NULL;
      delete q;

// newnode() -      Creates a new node with a zero as data
//            by default
pnode      list::newnode(void)
      pnode      p;

      p = new struct node;
      p -> data.itemcode[0] = '\0';
      p -> data.description[0] = '\0';
      p-> data.quantity = 0;
      p -> data.price = 0;

// newnode() -      Creates a new node with the parameter x
//            as its value
pnode      list::newnode(item x)
      pnode      p;

      p = new struct node;
      p -> data = x;
      p -> next = NULL;


// addfront() -      Inserts a new node containing x at
//            the front of the list
void      list::addfront(item x)
      pnode      p;

      p = newnode(x);
      p ->next = lstart;
      lstart = p;

// addrear() -       Inserts a new node containing x at
//            the rear of the list
void      list::addrear(item x)
      pnode      p, q;

      // Scan through the list to find the end
        // q points to the last node
      for (p = lstart, q= NULL; p != NULL; q = p, p = p -> next)      
      // Invariant - p must be NULL so we use it to hold a pointer
      // to the new node
      p = newnode(x);
      q -> next = p;

// insertafter() -      Insert value x in a new node to be inserted
//                  after p
void      list::insertafter(item x, pnode p)
      pnode       q;
      q = newnode(x);
      q -> next = p -> next;
      p -> next = q;

// isxthere() -      Is there a node on the list containing x?
bool      list::isxthere(char x[])
      pnode      p;

      if (lstart == NULL)

        // Scan through the list looking for x
      for (p = lstart; p != NULL && strcmp(p-> data.itemcode, x) != 0;
                  p = p -> next)
      // Invariant - either p contains x or we have gone through
        //      the entire list
      return((bool)(strcmp(p -> data.itemcode, x) == 0));

// getptr() - Get the pointer for the node containing x
pnode      list:: getptr(char x[])
      pnode      p;

        // Scan through the list looking for x
      for (p = lstart; p != NULL && strcmp(p-> data.itemcode, x) != 0;
                  p = p -> next)

      if (strcmp(p->data.itemcode, x) == 0)
              // p contains x
            // We searched through the whole list and
                //      x wasn't there

// removenode() -      Remove the node containing x from the list
void      list::removenode(char x[])
      pnode      p, q;

        // Scan through the list - is x there?
      for (p = lstart, q = NULL;  p != NULL && strcmp(p-> data.itemcode, x) != 0;
                   p = p -> next)      
            q = p;

        // If so, remove it
      if (strcmp(p->data.itemcode, x) == 0)      {
            if (q == NULL)
                  // x is at the front
                  // Re-adjust the pointer to the
                        // front of the list
                  lstart = p -> next;
                      // Splice it out of the list
                  q -> next = p -> next;

                // In either case, delete the node
            delete p;              

// write() - Write the data contents of every node on the list
void      list::write(void)
      pnode      p;

      if (lstart == NULL)
            cout << "The list is empty" << endl;
            for  (p = lstart; p != NULL; p = p -> next)
                  cout << p -> data.itemcode << '\t'
                        << p -> data.description << '\t'
                        << p -> data.quantity << '\t'
                        << p -> data.price << endl;

void      list::place(item x)
      pnode p, q;

      for (p = lstart, q = NULL;  p != NULL &&
                  strcmp(p -> data.itemcode, x.itemcode) <= 0;
                  q = p, p = p -> next)

      if (p == lstart)
            insertafter(x, q);

int      main(void)
      list      mylist;
      item      stuff;

      strcpy(stuff.itemcode, "123");
      strcpy(stuff.description, "widget");
      stuff.price = 5.01;
      stuff.quantity = 5;;


I used Retail price as:
Retail= Price + (Price * (10/100)).

Can anybody help me please.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't understand the question: Are you asking how to add information to your list (I think your code does that) and find/update elements in the list --or-- are you asking how to handle user input to turn this into a command-processing program?

If the latter you will want a loop and some strings (strings are usually easier for users to remember for commands). In main you'll want something like:

string cmd;

cout << "Command: ";
cin >> cmd;
while (cmd != "exit") {
  if (cmd == "add") {
    cout << "ItemCode: ";
    cin >> stuf.itemcode;
    // and so on for the other fields of stuff;
  } else if (cmd == "list") {
  } else {
    cout << "Unknown command " << cmd << endl;
  cout << "Command: ";
  cin >> cmd;

You'll need to include the right headers for string.

Hope this helps, -bcl
mlmcguireAuthor Commented:
I have four transactions to include in the linked list:

1.Taking-in-                updating the Quantity and price of the item(if it is in the list) or add it to the list if it is not.
2. Retail-                    sell x items for 105% of the price.
3. Order-form-            List all the inventory.
4. Quit.

Would I have to declare it in the main program.
There are two parts to the problem:

(1) Handling the different commands. You could use the structure I outlined above with any strings of your choosing as the command names and a branch in the n-way if statement for each of the commands.

(2) Making sure the inventory can support each command's functionality. I would suggest that you write functions, one for each command (except quit, probably) that manipulate the list according to the type of the command.

So, how should Taking-in- work? Well, you need to see if the named item is in the list; if it is then you can just change the quantity. If it isn't you can use the place operation to put the new product into the inventory.
Fortunately your linked list has the lookup ability built in already (good planning) and you just need to wire it up to be used when the user types in a product code.

A couple of things about your code:

(A) The standard (and most modern compilers) support the use of #include <iostream> (note, no .h) and #include <cstring> for the two files you include here. To use the string class you also need #include <string> (a different header file).

When you switch to the new include files you'll need to add one line AFTER the includes:

  using namespace std;

This lets you call standard output cout instead of by its full name std::cout.

(B) Using C++ strings is much, much easier than using C-style strings. I would urge you to read all about them and use them instead of char[] and char*. Easier to compare and they handle the \0 automatically and handle dynamic memory allocation correctly.

Hope this helps, -bcl

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept bcladd's comment as answer.

Please leave any comments here within the next four days.


EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.