strcpy and linked lists

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 22
Reputation: magikman is an unknown quantity at this point 
Solved Threads: 0
magikman magikman is offline Offline
Newbie Poster

strcpy and linked lists

 
0
  #1
Feb 25th, 2006
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.

  1. #include <iostream>
  2. #include <conio>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. char Descrip[25];
  9. double Price;
  10. int Quant;
  11. Node *Link;
  12. };
  13.  
  14. Node *Root = NULL;
  15.  
  16. void InsertFunction(char*, double, int, Node *);
  17.  
  18. int main( )
  19. {
  20. char InitDescrip[25];
  21. double InitPrice = 0;
  22. int InitQuant = 0;
  23. Node *Current = NULL;
  24. char Flag = 'Y';
  25.  
  26. while(toupper(Flag)=='Y')
  27. {
  28. cout << "Please enter the description of the part: ";
  29.  
  30. char nextchar = cin.peek();
  31. if(nextchar = '\n')cin.ignore();
  32. cin.get(InitDescrip, 24);
  33.  
  34. cout << "Please enter the price of the item: ";
  35. cin >> InitPrice;
  36.  
  37. cout << "Please enter the quantity in stock: ";
  38. cin >> InitQuant;
  39.  
  40. Current = new Node;
  41.  
  42. InsertFunction(InitDescrip, InitPrice, InitQuant, Current);
  43.  
  44. Root = Current;
  45.  
  46. cout << "More items to enter?";
  47. cin >> Flag;
  48.  
  49. }
  50.  
  51. cout << "Press any ket to terminate the program";
  52. getch( );
  53. return 0;
  54.  
  55.  
  56. }
  57.  
  58. void InsertFunction(char *InitDescrip, double InitPrice, int InitQuant, Node *Current)
  59. {
  60. Current->Descrip // <<--Where i am lost
  61. Current->Price = InitPrice;
  62. Current->Quant = InitQuant;
  63. Current->Link = Root;
  64. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strcpy and linked lists

 
0
  #2
Feb 25th, 2006
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
  1. strcpy(Current->Descrip,InitDescrip);
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: strcpy and linked lists

 
2
  #3
Feb 25th, 2006
Notice that you have a bug where you write,
  1. if(nextchar = '\n')cin.ignore();

I think you meant nextchar == '\n'.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC