C++ inventory

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7

91 Replies - 2873 Views - Last Post: 28 January 2013 - 12:14 AM Rate Topic: -----

#91 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,575
  • Joined: 19-February 09

Re: C++ inventory

Posted 27 January 2013 - 11:33 PM

I'm having second thoughts because I've forgotten about the condition of an item to be inserted into the start of a non-empty list.

If we go back to a while statement, which might be easier to understand, we can move the compare function into an if and use a break. I think this might be ok. The NULL was only used to explain, so not needed.

while(curr)
{
  curr->data.GetName(tempName);
  lowerCase(tempName);
  // if name not before alphabetically
  if( !(strcmp(tempName, newNodeName) < 0)) 
  { 
    break;
  }
  prev = curr;
  curr = curr->next;
}




The new Node is not always used, so should be deleted (or better created later than it is).

You could create a variable in inventory to hold the total weight.
Was This Post Helpful? 1
  • +
  • -

#92 sydewayzlocc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 18-April 12

Re: C++ inventory

Posted 28 January 2013 - 12:14 AM

inventory.cpp :


	#include <iostream>
	#include <iomanip>
	#include <cstring>
	#include "inventory.h"
    const int MAXCHAR = 101;
    const int MAXWEIGHT = 100;
	using namespace std;

	//private data members
	//Node *head;
	//Node *tail;

	//default constructor
	    inventory::inventory()
	{
            head = NULL;
	        tail = NULL;
	}

	    //copy constructor
	    inventory::inventory(const inventory& anInventory)
	    {
	        char tempName[MAXCHAR];

	        //anInventory is empty
	        if(anInventory.head == NULL)
	        {
	            head = NULL;
	        }
	        else
	        {
	            //copy the first node
	            head = new Node;
	            anInventory.head->data.GetName(tempName);
	            head->data.SetName(tempName);
	            head->data.SetWeight(anInventory.head->data.GetWeight());
	            head->data.SetCount(anInventory.head->data.GetCount());

	            //copy the rest of the linked list
	            Node *currSrc = anInventory.head->next;
	            Node *currDest = head;
	            while(currSrc)
	            {
	                currDest->next = new Node;
	                currDest = currDest->next;
	                currSrc->data.GetName(tempName);
	                currDest->data.SetName(tempName);
	                currDest->data.SetWeight(currSrc->data.GetWeight());
	                currDest->data.SetCount(currSrc->data.GetCount());

                currSrc = currSrc->next;
	            }
	            currDest->next = NULL;
	        }
	    }

	    //destructor
	    inventory::~inventory()
	    {
	        Node *curr;
	        if(head)
	        {
	            curr = head;
	        }
	        while(curr)
	        {
	            curr = curr->next;
	            delete head;
	            head = curr;
	        }
	        head = NULL;
	        curr = NULL;
	    }



    void inventory::AddItem(const item & anItem)
	    {
	        char tempName[MAXCHAR], newNodeName[MAXCHAR];
	        Node *newNode, *curr, *prev;

	        newNode = new Node;
	        anItem.GetName(newNodeName);
	        newNode->data.SetName(newNodeName);
	        lowerCase(newNodeName);
	        newNode->data.SetWeight(anItem.GetWeight());
	        newNode->next = NULL;

	        //check max weight
	        Node *tempcurr;
	        double totalWeight = 0;
	        for(tempcurr = head; tempcurr; tempcurr = tempcurr->next)
	        {
	            totalWeight += tempcurr->data.GetWeight();
	        }
	        if(newNode->data.GetWeight() <= MAXWEIGHT)
	        {
	            if(!head)
	            {
	                head = newNode;
	                tail = newNode;
	                newNode->data.SetCount(1);
	            }
	            else
	            {
	                //reset previous to NULL
	                prev = NULL;
	                curr = head;

                //check to see where to insert
	                curr->data.GetName(tempName);
	                lowerCase(tempName);
             	while(curr)
            {
                curr->data.GetName(tempName);
                lowerCase(tempName);
	  // if name not before alphabetically
                if( !(strcmp(tempName, newNodeName) < 0))
                {
                    break;
                }
                prev = curr;
                curr = curr->next;
	}

	                //increase weight and count
	                if(strcmp(tempName, newNodeName) == 0)
	                {
	                    curr->data.SetWeight(curr->data.GetWeight()+newNode->data.GetWeight());
	                    curr->data.SetCount(curr->data.GetCount()+1);
	                }
	                //add a new node
	                else
	                {
	                    newNode->next = curr;
	                    if(prev)
	                    {
	                        prev->next = newNode;
	                        newNode->data.SetCount(1);
	                    }
	                    else
	                    {
	                        head = newNode;
	                        newNode->data.SetCount(1);
	                    }
	                    if(!curr)
	                    {
	                        tail = newNode;
	                        newNode->data.SetCount(1);
	                        newNode->next = NULL;
	                    }
	                }
	            }
	        }
	        cout << "You picked up a " << newNodeName << "." << endl;
	    }


	    void inventory::RemoveItem(const char searchName[])
	    {
	        Node *curr, *prev;
	        char tempName[MAXCHAR];
	        char lowersearchName[MAXCHAR];
	        strcpy(lowersearchName, searchName);
	        bool found = false;
	        lowerCase(lowersearchName);
	        curr = head;
	        prev = NULL;
	        while(curr)
	        {
	            curr->data.GetName(tempName);
	            lowerCase(tempName);
	            if(strcmp(tempName, lowersearchName) == 0)
	            {
	                if(!head)
	                 cout << "Your inventory is empty. Nothing to remove!" << endl;
	                else
	                {
	                    if(prev)
	                        prev->next = curr->next;
	                    else
	                        head = curr->next;
	                    curr->next = NULL;
	                    delete curr;
	                    found = true;
	                }
	            }
	            prev = curr;
	            curr = curr->next;
	        }
	        if(found)
	        {
	            cout << "You dropped a " << searchName << " ." << endl;
	        }
	        if(!found)
	        {
	            cout << "You don't have a " << searchName << " in your inventory." << endl;
	        }
	    }


	    void inventory::PrintInventory() const
	    {
	        char    tempName[MAXCHAR];
	        int     totalItem = 0;
	        double  totalWeight = 0;

	        cout << endl;
	        cout << "Current inventory:" << endl;

	        if(!head)
	        {
	            cout << "\t(no items)" << endl;
	        }
	        else
	        {
	            Node *curr;
	            for(curr = head; curr; curr = curr->next)
	            {
	                curr->data.GetName(tempName);
	                cout << "\t[" << curr->data.GetCount() << "] " << tempName << endl;
	                totalItem += curr->data.GetCount();
	                totalWeight += curr->data.GetWeight();
	            }

	            cout << "Total items: " << totalItem << endl;
	            cout << "Total weight: " << totalWeight << endl;
	        }
	        cout << endl;
	    }


	    void lowerCase(char word[])
	    {
	        int i = 0;
	        while(word[i] != '\0')
	        {
	            word[i] = tolower(word[i]);
	            i++;
	        }
	    }





Everything is well Thank You for your help and dedication.
Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7