#include <string>
#include <cstdlib>

typedef enum {Dominion, Sobeys, Shoppers, Walmart} Store;

using namespace std;

class Item
{
private:
   // name of the item (non-empty string)
   string name;

   // number of dollars in price of item
   int dollars;

   // number of cents in price of item
   int cents;

   // store where item is sold
   Store whereToBuy;

   // number of items to buy
   int numToBuy;

public:

   // Default constructor for Item class.  Should set all fields to 0.
   Item();

   // Constructor for Item class
   Item(string name, int dollars, int cents, Store whereToBuy, int numtoBuy);

   // Returns the name of the item
   string getName();

   // Returns the cost of the item in dollars
   // Postcondition: dollars' >= 0
   //                cents' >= 0
   //                (dollars' > 0) or (cents' > 0)
   void getCost(int* dollars, int* cents);

   // Returns the store where the item is sold
   Store getStore();

   // Returns the quantity of the item to buyd
   int getNumToBuy();

   // Sets the name of the item
   // Precondition: name should be a non-empty string
   void setName(string name);

   // Sets the cost of the item in dollars
   // Precondition: (dollars >= 0)
   //               (cents >= 0)
   //               (dollars > 0) or (cents > 0)
   void setCost(int dollars, int cents);

   // Sets the store where the item is sold
   void setStore(Store store);

   // Sets the quantity of the item we want to buy
   // Precondition: numToBuy > 0
   void setNumToBuy(int numToBuy);
};

class ShoppingListNode
{
private:
    Item thisItem;
    ShoppingListNode* next;
public:
    ShoppingListNode(Item item)
    {
        thisItem = item;
        next = NULL;
    }
    ShoppingListNode(Item item, ShoppingListNode* nextNode)
    {
        thisItem = item;
        next = nextNode;
    }

};


class ShoppingList
{
private:
    ShoppingListNode* headNode;
public:

   // Creates an empty shopping list
   ShoppingList();

   // Constructor for a shopping list.
   // Precondition: items should point to an array of length numItems
   // Postcondition: After constructor is run, it should consist of numItem nodes with the items in the list the same as those in
   // the input array items and in that order.
   ShoppingList(Item* items, int numItems);

   // Constructor for a shopping list
   // Postcondition: (headNode->thisItem)' = item
   //                (headNode->next)' = restOfList
   ShoppingList(Item item, ShoppingListNode* restOfList);

   // Prints the names and prices of the items on the shopping list in
   // non-decreasing order of price per unit
   // Items should be printed out one per line - name first followed by a tab and then the price in the format $x.yz,
   // where x is the price in dollars, and yz is the price in cents (y and z are two decimal digits, and either or both may be 0).
   void printInOrder();

   // Returns the total cost of all items in the shopping list. Make sure that the individual price of each item in the list is multiplied by the
   // quantity of the item being purchased as given in the numToBuy field.
   // Postcondition: *dollars' >= 0
   //                *cents' >= 0
   //                *cents' < 100
   //                *dollars' = thisItem.dollars
   //                *cents' = thisItem.cents
   void getTotalCost(int* dollars, int* cents);

   // Adds item to the shopping list
   // Postcondition: If an item with the same name, location and price is already present in the list,
   // then the numToBuy field of the item in the list is incremented appropriately
   // Otherwise item is added as a fresh node to the list.
   bool addItem(Item item);

   // Removes item from the shopping list
   // Postcondition:  item is not present in the list after function execution
   // result is true if item was successfully removed
   // result is false if it wasn't in the list to begin with.
   bool removeItem(Item item);

   // Postcondition: result is true if the shopping list is empty and false otherwise.
   bool isEmpty();

   // Returns the number of stores where items have to be purchased from.
   // Postcondition: result >= 0
   int numStores();
};

This is .h file.

And following this is my cpp file . Please help me to resolve the printInOrder function, I have problems for reading the contents of everyNode, because the headNode->thisItem is not work, because the thisItem is in private.

Thank you

#include "ShoppingList.h"
#include <cassert>
#include <iostream>


Item::Item() {
    dollars = 0;
    cents = 0;
    numToBuy = 0;
}

Item::Item(string name, int dollars, int cents, Store whereToBuy, int numtoBuy) {
    name = name;
    dollars = dollars;
    cents = cents;
    whereToBuy = whereToBuy;
    numtoBuy = numToBuy;
}

string Item::getName() {
    return name;
}

void Item::getCost(int* dollars, int* cents) {
    if(*dollars == 0 && *cents == 0)
         cout << "error";
    cout << "$" + *dollars + '.' + *cents;
}

Store Item::getStore() {
    return whereToBuy;
}

int Item::getNumToBuy() {
    return numToBuy;
}

void Item::setName(string name) {
    assert(name!="");
    name = name;
}

void Item::setCost(int dollars, int cents) {
    double cost = 0.0;
    assert(dollars>=0 && cents>=0 && ((dollars > 0 && cents == 0) || (cents > 0 && dollars ==0)));
    cost = dollars + cents % 100;
}

void Item::setStore(Store store) {
    store = whereToBuy;
}

void Item::setNumToBuy(int numToBuy) {
    assert(numToBuy > 0);
    numToBuy = numToBuy;
}

ShoppingList::ShoppingList() {
    headNode = NULL;
}

//Constructor for a shopping list.
// Precondition: items should point to an array of length numItems
// Postcondition: After constructor is run, it should consist of numItem nodes with the items in the list the same as those in
// the input array items and in that order.  //
ShoppingList::ShoppingList(Item* items, int numItems) {
    ShoppingListNode* nextNode;
    headNode = new ShoppingListNode (*(items));
    for(int i = 0;i<numItems;i++){
        nextNode = new ShoppingListNode(*(items + i+1));
        nextNode = new ShoppingListNode (*(items+i),nextNode);
    }
}

// Constructor for a shopping list
// Postcondition: (headNode->thisItem)' = item
//                (headNode->next)' = restOfList
ShoppingList::ShoppingList(Item item, ShoppingListNode* restOfList) {
    headNode = new ShoppingListNode(item, restOfList);
}

// Prints the names and prices of the items on the shopping list in
// non-decreasing order of price per unit
// Items should be printed out one per line - name first followed by a tab and then the price in the format $x.yz,
// where x is the price in dollars, and yz is the price in cents (y and z are two decimal digits, and either or both may be 0).
void ShoppingList::printInOrder(){
     Item* item;
     ShoppingListNode* thisNode = headNode;
     ShoppingListNode* nextNode;
     int i = 0;
     //while(thisNode!= NULL){
        thisNode = new ShoppingListNode(*(item+i),nextNode);
        cout << (*(item+i)).getName();
        thisNode = nextNode;
     //}
}


bool ShoppingList::isEmpty() {
    if (headNode == NULL)
        return true;
    return false;
}

int ShoppingList::numStores(){
      Item tempI;
      Store tempS;
      int result1 = 0;
      int result2 = 0;
      int result3 = 0;
      int result4 = 0;
      ShoppingListNode* nextN = NULL;
      while(headNode != NULL){
          headNode = new ShoppingListNode(tempI,nextN);
          headNode = nextN;
          tempS = tempI.getStore();
          if(tempS == Dominion)
             result1 = 1;
          if(tempS == Sobeys)
             result2 = 1;
          if(tempS == Shoppers)
             result3 = 1;
          if(tempS == Walmart)
             result4 = 1;
      }
      return result1 + result2 + result3 + result4;
}


/************************Testing Main Function**************************/
int main() {
    int numItems;
    ShoppingList A;
    cout << "How many items do you want to buy? " << endl;
    cin >> numItems;
    Item* items;
    for(int i = 0;i<numItems;i++){
        *(items + i) = Item("Ada",1,2,Walmart,1);
        A(items,numItems);
    }
    A.printInOrder();
    return 0;
}

since thisitem is private you need to write a public contructor to access it and set a value.

to write a public contructor to access it and set

I cannot edit the header file. I can just only edit the cpp file.

thank you

#include "ShoppingList.h"
#include <cassert>
#include <iostream>
using namespace std;

Item::Item() {
    dollars = 0;
    cents = 0;
    numToBuy = 0;
}

Item::Item(string name, int dollars, int cents, Store whereToBuy, int numtoBuy) {
    name = name;
    dollars = dollars;
    cents = cents;
    whereToBuy = whereToBuy;
    numtoBuy = numToBuy;
}

string Item::getName() {
    return name;
}

void Item::getCost(int* dollars, int* cents) {
    if (*dollars == 0 && *cents == 0)
        cout << "error";
    cout << "$" + *dollars + '.' + *cents;
}

Store Item::getStore() {
    return whereToBuy;
}

int Item::getNumToBuy() {
    return numToBuy;
}

void Item::setName(string name) {
    assert(name!="");
    name = name;
}

void Item::setCost(int dollars, int cents) {
    double cost = 0.0;
    //assert(dollars>=0 && cents>=0 && ((dollars > 0 && cents == 0) || (cents > 0 && dollars ==0)));
    cost = dollars + cents % 100;
}

void Item::setStore(Store store) {
    store = whereToBuy;
}

void Item::setNumToBuy(int numToBuy) {
    assert(numToBuy > 0);
    numToBuy = numToBuy;
}

ShoppingList::ShoppingList() {
    headNode = NULL;
}


ShoppingList::ShoppingList(Item* items, int numItems) {
    ShoppingListNode* thisNode = headNode;
    for (int i = 0; i < numItems; i++) {
        thisNode -> thisItem = *(items + i);
        thisNode = thisNode->next;
    }
}

ShoppingList::ShoppingList(Item item, ShoppingListNode* restOfList) {
    headNode -> thisItem = item;
    headNode -> next = restOfList;
}


void ShoppingList::printInOrder() {
    ShoppingListNode* thisNode = headNode;
    //while (thisNode != NULL) {
        cout <<thisNode ->thisItem.getStore()<< "Name:  " << thisNode -> thisItem.getName() << endl;
        thisNode = thisNode ->next;
    //}
}


bool ShoppingList::addItem(Item item) {
    ShoppingListNode* thisNode = headNode;
    while (thisNode != NULL) {
        if (thisNode ->thisItem.getName() == item.getName()
                && thisNode->thisItem.getStore() == item.getStore()) {
            item.setNumToBuy(item.getNumToBuy() + 1);
            return false;
        }
        thisNode = thisNode -> next;
    }
    headNode = new ShoppingListNode(item, headNode);
    return true;
}

bool ShoppingList::removeItem(Item item) {
    if (!isEmpty()) {
        ShoppingListNode* thisNode = headNode;
        ShoppingListNode* nextNode = headNode -> next;
        if (thisNode -> thisItem.getName() == item.getName()
                && thisNode ->thisItem.getStore() == item.getStore()) {
            headNode = thisNode ->next;
            delete thisNode;
            return true;
        } else if (nextNode != NULL) {
            while (nextNode != NULL && nextNode ->thisItem.getName()
                    != item.getName() && nextNode ->thisItem.getStore()
                    != item.getStore()) {
                thisNode = thisNode -> next;
                nextNode = nextNode -> next;
            }
            if (nextNode != NULL && thisNode->thisItem.getName()
                    == item.getName() && thisNode->thisItem.getStore()
                    == item.getStore()) {
                ShoppingListNode* temp = nextNode -> next;
                thisNode -> next = temp;
                delete nextNode;
                return true;
            }
        }
    }
    return false;
}

bool ShoppingList::isEmpty() {
    if (headNode == NULL)
        return true;
    return false;
}

int ShoppingList::numStores() {
    Store store;
    ShoppingListNode* thisNode = headNode;
    int result1 = 0;
    int result2 = 0;
    int result3 = 0;
    int result4 = 0;
    while (thisNode != NULL) {
        store = thisNode ->thisItem.getStore();
        if (store == Dominion)
            result1 = 1;
        if (store == Sobeys)
            result2 = 1;
        if (store == Shoppers)
            result3 = 1;
        if (store == Walmart)
            result4 = 1;
        thisNode = thisNode -> next;
    }
    return result1 + result2 + result3 + result4;
}

Can someone can help me to check the errors

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.