944,090 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2291
  • C++ RSS
Feb 25th, 2006
0

strcpy and linked lists

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
magikman is offline Offline
28 posts
since Dec 2005
Feb 25th, 2006
0

Re: strcpy and linked lists

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
C++ Syntax (Toggle Plain Text)
  1. strcpy(Current->Descrip,InitDescrip);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Feb 25th, 2006
2

Re: strcpy and linked lists

Notice that you have a bug where you write,
C++ Syntax (Toggle Plain Text)
  1. if(nextchar = '\n')cin.ignore();

I think you meant nextchar == '\n'.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ Form Project - need to get HDC for drawing bitmaps
Next Thread in C++ Forum Timeline: get a random word





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC