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.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
as far as code like:
cout << "Enter First Name " << endl;
cin >> p.fName;
cout << "Enter last Name " << endl;
cin >> p.lName;
cout << "Enter Address " << endl;
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.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
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.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
I think you understand the concept. Here's your PERSON class (without the people array) but with some accessors added.
class PERSON
{
private:
char fName[25];
char lName[25];
char Address[100];
public:
PERSON();
char const * getFName() const;
char const * getLName() const;
char const * getAddress() const;
void setFName(char const * fname);
void setLName(char const * lname);
void setAddress(char const * address);
};
Here's an example implementation for a couple of those methods I added:
char const * PERSON::getFName() const
{
return fName;
}
void PERSON::setFName(char const * _fname)
{
strcpy_s(fName, sizeof(fName), _fname);
}
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
then the calls from main to reference the new methods:
case ADDPERSON :
cout << "Enter First Name " << endl;
cin >> fName;
cout << "Enter last Name " << endl;
cin >> lName;
cout << "Enter Address " << endl;
cin >> Address;
p.setFName(fName);
p.setLName(lName);
p.setAddress(Address);
status = access.addPerson(p);
You already had character buffers for the fName, lName and Address declared.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
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.
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116