954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

strcpy and linked lists

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;
}
magikman
Light Poster
29 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

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);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Notice that you have a bug where you write,

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


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

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You