Howdy,

I have been trying to teach myself C++ for a few months now and have been progressing nicely thanks to all the wonderful help I have received on this site and others. Thanks for your help!

Now, on to my question. I am currently trying to learn linked lists. I think I have the general concept down. However, I am currently trying to figure out how to deal with arrays that are part of the data field of my links. In this particular example I am dealing with an array of characters. My problem is that I am not too sure how to use strcpy in this situation. In my example I have included all the code that I have so far. Thanks for your help.

#include <iostream>
#include <conio>

using namespace std;

struct Node
{
        char Descrip[25];
        double Price;
        int Quant;
        Node *Link;
};

Node *Root = NULL;

void InsertFunction(char*, double, int, Node *);

int main( )
{
        char InitDescrip[25];
        double InitPrice = 0;
        int InitQuant = 0;
        Node *Current = NULL;
        char Flag = 'Y';

        while(toupper(Flag)=='Y')
        {
                cout << "Please enter the description of the part: ";

                char nextchar = cin.peek();
                if(nextchar = '\n')cin.ignore();
                cin.get(InitDescrip, 24);

                cout << "Please enter the price of the item: ";
                cin >> InitPrice;

                cout << "Please enter the quantity in stock: ";
                cin >> InitQuant;

                Current = new Node;

                InsertFunction(InitDescrip, InitPrice, InitQuant, Current);

                Root = Current;

                cout << "More items to enter?";
                cin >> Flag;

        }

        cout << "Press any ket to terminate the program";
        getch( );
        return 0;


}

void InsertFunction(char *InitDescrip, double InitPrice, int InitQuant, Node *Current)
{
        Current->Descrip   //  <<--Where i am lost
        Current->Price = InitPrice;
        Current->Quant = InitQuant;
        Current->Link = Root;
}

since this is a c++ program, use std::string instead of char arrays. std::string is a lot easier to use.

struct Node
{
        std::string Descrip;
        double Price;
        int Quant;
        Node *Link;
};

...
void InsertFunction(char *InitDescrip, double InitPrice, int InitQuant, Node *Current)
{
        Current->Descrip  = InitDescrip; 
        Current->Price = InitPrice;
        Current->Quant = InitQuant;
        Current->Link = Root;
}

of course if you must use those C char arrays, then do this

strcpy(Current->Descrip,InitDescrip);
This article has been dead for over six months. Start a new discussion instead.