converting Address Book struct to class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 11
Reputation: kitty7 is an unknown quantity at this point 
Solved Threads: 0
kitty7 kitty7 is offline Offline
Newbie Poster

converting Address Book struct to class

 
0
  #1
Dec 15th, 2008
Okay, well, I got my Address Book project done on time My current project is to turn it into a class. I have been working on this for a while and I keep getting set back to square one. My biggest problem is with writing some sort of user interface while dealing with private variables. I have read some posts by the Queen of Daniweb, which were helpful, but I can't get to her tutorial.... anyway, if someone could just explain how a person interfaces with private variables, I would appreciate it. Just something to get me started. Anyway, here's the code so far:



  1. // Header file first
  2.  
  3.  
  4. #ifndef _ADDRESSBOOK
  5. #define _ADDRESSBOOK
  6.  
  7. const int MAXADDRESS = 25;
  8.  
  9. class PERSON
  10. {
  11. private:
  12. char fName[25];
  13. char lName[25];
  14. char Address[100];
  15. char people[MAXADDRESS];
  16.  
  17. public:
  18.  
  19. bool addPerson(const PERSON &p);
  20. bool getPerson(PERSON &p);
  21. bool findPerson(char *lastName, PERSON &p);
  22. bool findPerson(char *lastName, char *firstName, PERSON &p);
  23. void printBook(PERSON &p);
  24. };
  25.  
  26.  
  27. #endif
  28.  
  29. //Now the cpp with all the functions...
  30.  
  31. #include <iostream>
  32. using namespace std;
  33. #include "definitionsAddressBook.h"
  34.  
  35. using namespace std;
  36.  
  37. int head = 0;
  38. int tail = -1;
  39.  
  40.  
  41. bool PERSON::addPerson(const PERSON &p)
  42. {
  43.  
  44. if(head < MAXADDRESS)
  45. {
  46.  
  47. people[head] = p;
  48. head++;
  49. if(tail == -1)
  50. tail++;
  51. return true;
  52. }
  53. return false;
  54. }
  55. bool PERSON::getPerson(PERSON &p)
  56. {
  57.  
  58. if(tail >=0)
  59. {
  60.  
  61. if(tail >= MAXADDRESS)
  62. tail = 0;
  63. p = people[tail];
  64. tail++;
  65.  
  66. return true;
  67.  
  68. }
  69. return false;
  70. }
  71. bool PERSON::findPerson(char *lastName, PERSON &p)
  72. {
  73.  
  74. for(int i = 0; i < head; i++)
  75. {
  76. if(!_stricmp(people[i].lName, lastName))
  77.  
  78. {
  79. p = people[i];
  80. return true;
  81.  
  82. }
  83.  
  84. }
  85. return false;
  86. }
  87. bool PERSON::findPerson(char *lastName, char *firstName, PERSON &p)
  88. {
  89.  
  90. for(int i = 0; i < head; i++)
  91. {
  92.  
  93. if(!_stricmp(people[i].lName, lastName) && !_stricmp(people[i].fName, firstName))
  94.  
  95. {
  96. p = people[i];
  97. return true;
  98.  
  99. }
  100.  
  101. }
  102. return false;
  103. }
  104. void printBook(PERSON &p)
  105. {
  106.  
  107.  
  108. for(int i = 0; i < MAXADDRESS; i++)
  109. {
  110.  
  111. cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << endl;
  112.  
  113. }
  114.  
  115. }
  116.  
  117.  
  118.  
  119. // Now the main cpp
  120.  
  121. #include <iostream>
  122. using namespace std;
  123.  
  124. #include "definitionsAddressBook.h"
  125. #include "conio.h"
  126.  
  127.  
  128. using namespace std;
  129. int printMenu();
  130. void waitKey();
  131.  
  132. const int ADDPERSON = 1;
  133. const int GETPERSON = 2;
  134. const int FINDLAST = 3;
  135. const int FINDBOTH = 4;
  136. const int PRINT = 5;
  137. const int EXIT = 0;
  138. int main()
  139. {
  140.  
  141. int selection;
  142. PERSON p;
  143. PERSON access;
  144. bool status;
  145. char fName[50];
  146. char lName[50];
  147. char Address[100];
  148. char *firstName;
  149. char *lastName;
  150. char *address2;
  151.  
  152.  
  153.  
  154. selection = printMenu();
  155. while(selection != EXIT )
  156. {
  157. switch(selection)
  158. {
  159. case ADDPERSON :
  160.  
  161. cout << "Enter First Name " << endl;
  162. cin >> p.fName;
  163. cout << "Enter last Name " << endl;
  164. cin >> p.lName;
  165. cout << "Enter Address " << endl;
  166. cin >> p.Address;
  167.  
  168.  
  169. status = access.addPerson(p);
  170. if(status == false)
  171. cout << "Sorry There is no more room in the address book " << endl;
  172.  
  173. else
  174. cout << "Thanks for your Entry " << endl;
  175.  
  176.  
  177. waitKey();
  178. break;
  179.  
  180. case GETPERSON :
  181.  
  182. status = access.getPerson(p);
  183. if(status)
  184. cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
  185. else
  186. cout << "Sorry The address book is empty " << endl;
  187.  
  188. waitKey();
  189. break;
  190.  
  191.  
  192. case FINDLAST :
  193.  
  194.  
  195. cout << "Enter a last name " << endl;
  196. cin >> lName;
  197. status = access.findPerson(lName, p);
  198.  
  199. if(status)
  200. cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
  201. else
  202. cout << "Sorry, Name not found " << endl;
  203. waitKey();
  204.  
  205. break;
  206.  
  207.  
  208. case FINDBOTH :
  209.  
  210. cout << "Enter last name " << endl;
  211. cin >> lName;
  212. cout << "Enter first name " << endl;
  213. cin >> fName;
  214. status = access.findPerson(lName, fName, p);
  215.  
  216. if(status)
  217. cout << p.fName << "\t" << p.lName << " " << p.Address << endl;
  218. else
  219. cout << "Sorry, Name not found " << endl;
  220.  
  221. waitKey();
  222. break;
  223.  
  224.  
  225. case PRINT :
  226.  
  227. printBook();
  228. waitKey();
  229.  
  230. break;
  231. case EXIT :
  232.  
  233. cout << "Thanks for using the address book " << endl;
  234. exit(0);
  235.  
  236. }
  237. selection = printMenu();
  238.  
  239. }
  240. }
  241.  
  242. int printMenu()
  243. {
  244.  
  245. int selection;
  246.  
  247.  
  248. system("CLS");
  249.  
  250. cout << "1. Add A Person" << endl;
  251. cout << "2. Get A Person " << endl;
  252. cout << "3. Find A person By Last Name " << endl;
  253. cout << "4. Find A person By First and Last Name " << endl;
  254. cout << "5. Print the address book" << endl;
  255. cout << "0. Exit this program " << endl;
  256. cin >> selection;
  257.  
  258. return selection;
  259.  
  260. }
  261.  
  262. void waitKey()
  263. {
  264.  
  265. cout << "Press a key to continue " << endl;
  266.  
  267. while(!kbhit())
  268. ;
  269.  
  270. getch();
  271. fflush(stdin);
  272.  
  273. }

Anyway, I can't have any cin or cout in the functions - beileve me, I already tried. If anybody has a suggestion, I would really appreciate it.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #2
Dec 15th, 2008
The code as posted has a few problems:

In combination with const int MAXADDRESS = 25; , the statement char people[MAXADDRESS]; declares an array of 25 characters.

But later in methods of the class, you treat people like it was an array of PERSON.

See: people[head] = p; and p = people[tail]; for examples. (This shouldn't even compile, doesn't your compiler complain about it?)

You also have methods which you appear to intend to 'return' a value through a PERSON reference. For example: bool PERSON::findPerson(char *lastName, PERSON &p) appears to search the people array (treating it like an array of PERSON) for a name that matches and assigns to p from the PERSON array. As long as you are aware that at the point of that assignment, the contents of whatever were passed in as 'p' have been overwritten with a copy of the record found, everything is fine. I just wanted to make sure you understood how it works.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #3
Dec 15th, 2008
as far as code like:
  1. cout << "Enter First Name " << endl;
  2. cin >> p.fName;
  3. cout << "Enter last Name " << endl;
  4. cin >> p.lName;
  5. cout << "Enter Address " << endl;
  6. cin >> p.Address;
where p is an instance of PERSON, it is generally not allowed.
Your options are:
1) make the members public. (bad form, but you could make it work.)
2) add accessors for the dataitems. Methods like setFName(char const * fname) that will allow you to set the data members. You would have to accept user input into temporary variables and then call the accessor to set the data.
3) add an update that takes all of the fields that might be updated in one call. (Similar to the previous, you still need to use temporary variables.)
4) add a method that would prompt for and accept input into the members. (I think you said you couldn't do that, but I don't know if it was because you couldn't make it work, or if it was not permitted.)

#2 tends to be the most common implementation.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #4
Dec 15th, 2008
On one more design note, it looks like you had PERSON and then attempted to add an array to it to make the address book.

I would recommend having an ADDRESSBOOK class that has the array and the members for updating and accessing the PERSON objects in the ADDRESSBOOK.
Last edited by Murtan; Dec 15th, 2008 at 3:18 am. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: kitty7 is an unknown quantity at this point 
Solved Threads: 0
kitty7 kitty7 is offline Offline
Newbie Poster

Re: converting Address Book struct to class

 
0
  #5
Dec 15th, 2008
I apologize for the errors concerning people. I only recently found out that I had to include it in the private variables section. This is also my first class, so please bear with me. Now, as for your suggestion, I think #2 is the only one I would be able to do. 1 and 4 are out of the question as my professor does not like cout or cin in any function besides main. How would I begin to implement this? We learned pointers the week before last, so its not my strong point, but I think I could do it. If I am correctly interpreting what you are saying, this plan would be to make different variables for every variable in the private section of the class.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: kitty7 is an unknown quantity at this point 
Solved Threads: 0
kitty7 kitty7 is offline Offline
Newbie Poster

Re: converting Address Book struct to class

 
0
  #6
Dec 15th, 2008
The array was originally global. I had to put it inside of the class and I have not figured out how to change the rest of the program respectively.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #7
Dec 15th, 2008
I think you understand the concept. Here's your PERSON class (without the people array) but with some accessors added.

  1. class PERSON
  2. {
  3. private:
  4. char fName[25];
  5. char lName[25];
  6. char Address[100];
  7.  
  8. public:
  9. PERSON();
  10. char const * getFName() const;
  11. char const * getLName() const;
  12. char const * getAddress() const;
  13. void setFName(char const * fname);
  14. void setLName(char const * lname);
  15. void setAddress(char const * address);
  16. };

Here's an example implementation for a couple of those methods I added:
  1. char const * PERSON::getFName() const
  2. {
  3. return fName;
  4. }
  5. void PERSON::setFName(char const * _fname)
  6. {
  7. strcpy_s(fName, sizeof(fName), _fname);
  8. }
Last edited by Murtan; Dec 15th, 2008 at 3:38 am. Reason: better to call strcpy_s
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #8
Dec 15th, 2008
then the calls from main to reference the new methods:

  1. case ADDPERSON :
  2.  
  3. cout << "Enter First Name " << endl;
  4. cin >> fName;
  5. cout << "Enter last Name " << endl;
  6. cin >> lName;
  7. cout << "Enter Address " << endl;
  8. cin >> Address;
  9.  
  10. p.setFName(fName);
  11. p.setLName(lName);
  12. p.setAddress(Address);
  13.  
  14. status = access.addPerson(p);

You already had character buffers for the fName, lName and Address declared.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: kitty7 is an unknown quantity at this point 
Solved Threads: 0
kitty7 kitty7 is offline Offline
Newbie Poster

Re: converting Address Book struct to class

 
0
  #9
Dec 15th, 2008
Thank you for your demonstration, this is helpful. I am curious, is this some kind of a constant character pointer? Wouldn't it need to be an array? I don't know very much about this, but it seems to me that a pointer won't hold anything; it will just point to an address...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: converting Address Book struct to class

 
0
  #10
Dec 15th, 2008
Yes the getFName did return a constant character pointer. All pointers can implicitly be treated as an array (you just have to be careful not to write where you don't have permission). In this case, it points to the buffer inside the PERSON object and is a pointer to a const char. (The const part encourages the compiler to make it hard for you to write through the pointer accidentally.) You can use this pointer in your code like you would any character array. (Specifically, you can cout << p.getFName(); and will output the first name, just like you might expect it to.)

We don't really need to make a copy of the string unless we plan to modify it. And if we do, we can make the copy using the pointer.

The same thing works in reverse for the setFName() method, except the only thing we do with the pointer is to make a copy of what it pointed to into our buffer.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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