| | |
strcpy and linked lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#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.
of course if you must use those C char arrays, then do this
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
C++ Syntax (Toggle Plain Text)
strcpy(Current->Descrip,InitDescrip);
Notice that you have a bug where you write,
I think you meant nextchar == '\n'.
C++ Syntax (Toggle Plain Text)
if(nextchar = '\n')cin.ignore();
I think you meant nextchar == '\n'.
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- Linked Lists in C (C)
- Concordance, Linked Lists and classes (C++)
- help with linked lists (C++)
- Singly-Linked Lists: Ultimate (C++)
- Bug when creating linked lists in Dev C++ (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ Form Project - need to get HDC for drawing bitmaps
- Next Thread: get a random word
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






