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;
}

Dani AI

Generated

A few focused, practical points to add to the thread for — and to build on and :

Using std::string is the cleanest fix (automatic memory, safe assignment, easy full-line input with std::getline). If you keep a fixed-size char array inside the node, copy safely to avoid overflow: use a bounded copy and force a terminating NUL byte.

#include <cstring>

std::strncpy(Current->Descrip, InitDescrip, sizeof(Current->Descrip) - 1);
Current->Descrip[sizeof(Current->Descrip) - 1] = '\0';

Notes: strncpy does not always NUL-terminate when the source is too long, so explicitly set the last byte. Prefer sizeof(Current->Descrip) (works here because Descrip is an actual array in the struct). If you pass the buffer as a char* parameter, pass the buffer length separately — sizeof will not work on a pointer.

Input handling: mixing formatted extraction (operator>>) and line-based input commonly leaves a newline in the stream. Clear it before reading full lines:

#include <limits>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // drop leftover
std::getline(std::cin, someStdString);

Or use std::cin >> std::ws; before std::getline to skip leading whitespace.

Linked-list and memory tips: keep the insertion semantics clear — set the new node’s Link to the current head and then update the head. Either let the insert routine allocate the node (encapsulation) or, if you allocate outside, make sure the function documents who owns the pointer afterward. Remember to release nodes when done. Also avoid nonstandard headers like <conio.h>/getch() for portable code.

These changes address the strcpy/overflow concern, the input/newline issue that spotted, and make the node code more robust as suggested.

Recommended Answers

All 2 Replies

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);

Notice that you have a bug where you write,

if(nextchar = '\n')cin.ignore();

I think you meant nextchar == '\n'.

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.