Please see my comments in the code.
//Structure definition Person.
//contains definitions for Functionality.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct PERSON
{
char firstName[20];
char lastName[20];
char address[50];
};
const int MAXPEOPLE = 2;
PERSON people[MAXPEOPLE];
PERSON p; // you dont need this global variable
//.cpp file
//Contains functionality of addressBook
#include <iostream>
using namespace std;
#include "definitionsAddressBook.h"
int addPerson(PERSON p);
int getPerson(PERSON p);
int addPerson(PERSON p)
{
for(int i = 0; i < MAXPEOPLE; i++)
{
/* 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? */
PERSON people[MAXPEOPLE];
people[0] = p;
}
return 0;
}
int getPerson(PERSON p)
{
/* are you using 'p'( function argument) anywhere? if no then why add it to the function signature? */
for(int i = 0; i < MAXPEOPLE; i++)
{
people[i];
}
return 0;
}
int main()
{
cout << "This is an address book that can hold ten people. " << endl;
for(int i = 0; i < MAXPEOPLE; i++)
{
/* 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. */
cout << "Please enter the first name: " << endl;
cin >> p.firstName;
cout << "Please enter the last name: " << endl;
cin >> p.lastName;
cout << "Now enter the address: " << endl;
cin >> p.address;
addPerson(p);
}
/*are you using the parameter anywhere in the function?*/
getPerson(p);
/*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*/
cout << people << endl;
}
Last edited by Agni; Dec 1st, 2008 at 6:01 am.
thanks
-chandra