View Single Post
Join Date: Dec 2007
Posts: 439
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: A Puzzling Address Book...

 
0
  #3
Dec 1st, 2008
Please see my comments in the code.

  1. //Structure definition Person.
  2. //contains definitions for Functionality.
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6. using namespace std;
  7.  
  8.  
  9. struct PERSON
  10. {
  11. char firstName[20];
  12. char lastName[20];
  13. char address[50];
  14.  
  15. };
  16. const int MAXPEOPLE = 2;
  17. PERSON people[MAXPEOPLE];
  18. PERSON p; // you dont need this global variable

  1. //.cpp file
  2. //Contains functionality of addressBook
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. #include "definitionsAddressBook.h"
  7.  
  8. int addPerson(PERSON p);
  9. int getPerson(PERSON p);
  10.  
  11.  
  12. int addPerson(PERSON p)
  13. {
  14. for(int i = 0; i < MAXPEOPLE; i++)
  15. {
  16. /* this is a variable local to this loop. every time this function gets called a new array is created and you then add the element to the 0th location of this array. Do you need this or the global array declared in .h? */
  17.  
  18. PERSON people[MAXPEOPLE];
  19. people[0] = p;
  20.  
  21. }
  22.  
  23. return 0;
  24.  
  25. }
  26. int getPerson(PERSON p)
  27. {
  28. /* are you using 'p'( function argument) anywhere? if no then why add it to the function signature? */
  29.  
  30. for(int i = 0; i < MAXPEOPLE; i++)
  31. {
  32. people[i];
  33. }
  34.  
  35. return 0;
  36. }
  37.  
  38. int main()
  39. {
  40. cout << "This is an address book that can hold ten people. " << endl;
  41.  
  42. for(int i = 0; i < MAXPEOPLE; i++)
  43. {
  44. /* you can declare a variable of type person here and use that, everytime a new struct will be created which will passed by value to the addPerson function. */
  45.  
  46. cout << "Please enter the first name: " << endl;
  47. cin >> p.firstName;
  48. cout << "Please enter the last name: " << endl;
  49. cin >> p.lastName;
  50. cout << "Now enter the address: " << endl;
  51. cin >> p.address;
  52. addPerson(p);
  53. }
  54.  
  55. /*are you using the parameter anywhere in the function?*/
  56. getPerson(p);
  57.  
  58. /*probably you can think of writing this cout in someother function, it should print not just one person but the entire array one -by-one*/
  59. cout << people << endl;
  60.  
  61. }
Last edited by Agni; Dec 1st, 2008 at 6:01 am.
thanks
-chandra
Reply With Quote