so i have some code that ive been working on, and dare i say, understanding, involving linked lists, only i've hit a wall

I need to add a new structure, called date, so that a day, month, and year, are associated with every node, for rolls, jam, and tea, and then there is a specific "expiration date" associated with each

heres the code

#include "stdafx.h"
#include <iostream> 
#include <cstddef> 
#include <string> 
using namespace std; 

struct Node 
{ 
    string item; 
    int count; 
    Node *link; 
}; 
typedef Node* NodePtr; 
void head_insert(NodePtr& head, string an_item, int a_number); 
void show_list(NodePtr& head); 
int main() 
{ 
    NodePtr head = NULL; 
    head_insert(head, "Tea", 2); 
    head_insert(head, "Jam", 3); 
    head_insert(head, "Rolls", 10); 
    show_list(head); 
    return 0; 
} 
void head_insert(NodePtr& head, string an_item, int a_number) 
{ 
    NodePtr temp_ptr; 
    temp_ptr = new Node; 
    temp_ptr-> item = an_item; 
    temp_ptr-> count = a_number; 
    temp_ptr->link = head; 
    head = temp_ptr; 
} 
void show_list(NodePtr& head) 
{ 
    NodePtr here = head; 
    while (here != NULL) 
    { 
  cout << here-> item << "\t"; 
  cout << here-> count << endl; 
        here = here->link; 
    } 
}

i somewhat understand the syntax to add in a new structure
but then having to put in the expiration dates...day month and year...how would i declare those? ints? doubles?

struct Date
{
   int day;
   int month;
   int year;
   void display(){cout << day << '/' << month << '/' << year;}
};

struct Node 
{ 
    string item; 
    int count; 
    Date expirationDate;
    Node *link; 
}; 

Node * newNode = new Node;
newNode->expirationDate.day = 1;
newNode->expirationDate.month = 1;
newNode->expirationDate.year = 2010;
newNode->expirationDate.display();

void show_list(NodePtr& head) 
{ 
    NodePtr here = head; 
    while (here != NULL) 
    { 
        cout << here-> item << "\t"; 
        cout << here-> count << "\t";
        here->expirationDate.display();
        cout << endl; 
        here = here->link; 
    } 
}

The other option, which is convenient, is to overload the << for the user defined variable you want to display rather than using a method to do it.

how would i go about setting a different expiration date for each of the items?

im getting these errors on lines 26-29

Error 1 error C2143: syntax error : missing ';' before '->'

Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Error 3 error C2040: 'newNode' : 'int' differs in levels of indirection from 'Node *'

not really sure what those mean

heres how i have the code now, perhaps i did something wrong?

#include "stdafx.h"
#include <iostream> 
#include <cstddef> 
#include <string> 

using namespace std; 

struct Date
{
   int day;
   int month;
   int year;
   void display(){cout << day << '/' << month << '/' << year;}
};

struct Node 
{ 
    string item; 
    int count; 
    Date expirationDate;
    Node *link; 
}; 

Node * newNode = new Node;
newNode->expirationDate.day = 1;
newNode->expirationDate.month = 1;
newNode->expirationDate.year = 2010;
newNode->expirationDate.display();

void show_list(NodePtr& head) 
{ 
    NodePtr here = head; 
    while (here != NULL) 
    { 
        cout << here-> item << "\t"; 
        cout << here-> count << "\t";
        here->expirationDate.display();
        cout << endl; 
        here = here->link; 
    } 
}

struct Node 
{ 
    string item; 
    int count; 
    Node *link; 
}; 
typedef Node* NodePtr; 
void head_insert(NodePtr& head, string an_item, int a_number); 
void show_list(NodePtr& head); 
int main() 
{ 
    NodePtr head = NULL; 
    head_insert(head, "Tea", 2); 
    head_insert(head, "Jam", 3); 
    head_insert(head, "Rolls", 10); 
    show_list(head); 
    return 0; 
} 
void head_insert(NodePtr& head, string an_item, int a_number) 
{ 
    NodePtr temp_ptr; 
    temp_ptr = new Node; 
    temp_ptr-> item = an_item; 
    temp_ptr-> count = a_number; 
    temp_ptr->link = head; 
    head = temp_ptr; 
} 
void show_list(NodePtr& head) 
{ 
    NodePtr here = head; 
    while (here != NULL) 
    { 
  cout << here-> item << "\t"; 
  cout << here-> count << endl; 
        here = here->link; 
    } 
}

You created duplicate version of Node and showlist copy/pasted my first post. here's a rough version of how you might structure the progam. It has not been compiled or tested so there may be errors. I would probably modularize things more, and I would increase the security of the data if I were doing this for something other than my own edification.

struct Date
{
   int day;
   int month;
   int year;
   void display(){cout << day << '/' << month << '/' << year;}
};

struct Node 
{ 
    string item; 
    int count; 
    Date expirationDate;
    Node * next; 
}; 

void showList(Node * head) 
{ 
    Node * here = head; 
    while (here != NULL) 
    { 
        cout << here-> item << "\t"; 
        cout << here-> count << "\t";
        here->expirationDate.display();
        cout << endl; 
        here = here->link; 
    } 
}

void buildList(Node * head)
{
   string i;
   int c, d, m, y;
   bool addAnotherNode = true;
   while(addAnotherNode)
   {
       cout << "enter item, count" << endl;
       cin >> i >> c >> d >> m >> y;
       insertAtHead(head, i, c, d, m, y);
       cout << "add another Node:  y/n"  << endl;
       cin >> choice;
       if(choice == 'n')
         addAnotherNode = false;
    }
}

void insertAtHead(Node * & head,  string i, int c, int d, int m, int y)
{
    //declare memory for a new Node
    Node * newNode = new Node();

    //populate new Node with data passed in
    newNode->item = i;
    newNode->count = c;
    newNode->expirationDate.day = d;
    newNode->expirationDate.month = m;
    newNode->expirationDate.year = y;
    newNode->next = NULL;

    if(head == NULL)
      head = newNode;
    else
    {
      newNode->next = head;
      head = newNode;
    }
}

int main()
{
    //declare a list
    Node * head = NULL;
    head.buildList();
    head.showList();
    cin.get();
    cin.get();
    return 0;
}

have you tried using different link list?

uhh what? different link list?

i cant stand this stuff its too hard haha idk why theyre making me take it im a networking major...im gonna fail haha

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.