Hi Everyone,
I have this final assignment and I have so many difficulties getting it done. I'm supposed to write/code an address book where I could add, delete, display and sort data. I'm not too familiar with C++ as this is my first semester taking a programming course. This is what I have ready but it wont run and I couldnt add the sorting data function. I need help please I have till 6pm. Thank you all in advance.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Person
{
 			 string name; // The name of record management system
 			 string addr; // The number of contacts
};

// functin prototypes
void addData(AddressBook list[], int& size);
void dispData(const AddressBook list[], int size); 
void saveFile(const AddressBook list[], int size);
void openFile(AddressBook list[], int& size);
char getMenuResponse();			 
int main(int argc, char *argv[])
{
  AddressBook contactList[MAX_SIZE];
  int numOfContacts = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(contactList, numOfContacts); break;
  		case 'D': dispData(contactList, numOfContacts); break;
  		case 'O': openFile(contactList, numOfContacts); break;
  		case 'S': saveFile(contactList, numOfContacts); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
  
  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}

void addData(AddressBook list[], int& size)
{
  AddressBook tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
		// see if this record should be added to the array
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const Address Book list[], int size)
{
  system("cls");
  
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;
        
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(10)  << list[i].addr<<left<<endl;
    }
    
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }
  
  system("PAUSE");
  system("cls");
}

// Save records to disc
void saveFile(const AddressBook list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());
  
  // to make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";
    
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}

// Open file and load array
void openFile(AddressBook list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading Address Book from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
      
      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].addr;
  
    }
    cout << endl << size << " Contacts read from the disc." << endl;

  
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}

char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}

When you say it "won't run", do you mean it doesn't compile? Or that it crashes? Or that it runs, but not correctly? Can you be more specific?

The first thing I notice is that the AddressBook type or class isn't defined anywhere. Is Person supposed to be AddressBook ?

Hey,
I meant it tells me there are some errors. You are right it tells me I have not declared addressbook in the scope. By person I meant contact shall I replace person by contact then? will that do the declaration part? I'm really confused and this material is harder to me than chem and math combined.

OK, then. Where is AddressBook declared, if not here? Is it declared at all? What are the members of the structure?

I notice a few other things like this all through the code. For example, at one place you have it as "Address Book" (with the space), which won't compile even if everything else were correct.

I am sorry but I dont seem to understand your questions. I thought AddressBook was declared here AddressBook contactList[MAX_SIZE] if not then I am totally misunderstanding what declaration is. I'm just a beginner and this stuff is really hard for me so I hope you can give me some hints and help me get this done before the deadline. Thank you so much for ur responses

I am sorry but I dont seem to understand your questions. I thought AddressBook was declared here AddressBook contactList[MAX_SIZE] if not then I am totally misunderstanding what declaration is.

Yes, that is a declaration of a specific variable of the type AddressBook ; but you would also need to define the struct type AddressBook before declaring any objects of that type. A typical definition for a struct would be:

struct AddressBook
{
    std::string contactName;
    std::string address;
    std::string phone;
    std::string email;
};

Something like that would be needed before you could declare a variable of type AddressBook . The structure definition describes what an AddressBook object is composed of.

Edited 4 Years Ago by Schol-R-LEA: n/a

Hey here is my code I kind of fixed it up a bit and ur hints were really helpful. It finally compiles now but when I display contacts it only shows names and email addresses only. How can I fix that?

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Person
{
            string name;
            string addr;
                };

const int MAX_SIZE = 8; // max number of contacts

// functin prototypes
void addData(Person list[], int& size);
void dispData(const Person list[], int size); 
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();          
int main(int argc, char *argv[])
{
  Person contactList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
    switch ( getMenuResponse() ) 
    {
        case 'A': addData(contactList, numOfRecs); break;
        case 'D': dispData(contactList, numOfRecs); break;
        case 'O': openFile(contactList, numOfRecs); break;
        case 'S': saveFile(contactList, numOfRecs); break;
        case 'Q': run = false; break;
        default : cout << "That is NOT a valid choice" << endl;
    }
  } while (run);
  cout << endl << "Program Terminated" << endl;

  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}

void addData(Person list[], int& size)
{
  Person tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << endl;
        cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
        // see if this record should be added to the array
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const Person list[], int size)
{
  system("cls");

  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;

    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(10)  << list[i].addr<<left<<endl;
    }

    cout << "********************************************" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }

  system("PAUSE");
  system("cls");
}

// Save records to disc
void saveFile(const Person list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());

  // to make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";

    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}

// Open file and load array
void openFile(Person list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;

  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 

    system("cls");
    cout << "Reading Address Book from the disc ";

    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;

      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].addr;

    }
    cout << endl << size << " Contacts read from the disc." << endl;


    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}

char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
    char response;
    cout << endl << "Make your selection" << endl
         << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
         << "> ";
    cin >> response;
    cin.ignore(256, '\n');  
    // clean-up up to 256 chars including the delimiter specified (\n, the endl) 
    // OR stop when the \n is encountered after removing it.
    return toupper(response);
    // note the use of toupper, why?
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Im sorry didnt post the code correctly here it is again:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Person
{
 			string name;
 			string addr;
				};

const int MAX_SIZE = 8; // max number of contacts

// functin prototypes
void addData(Person list[], int& size);
void dispData(const Person list[], int size); 
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();			 
int main(int argc, char *argv[])
{
  Person contactList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(contactList, numOfRecs); break;
  		case 'D': dispData(contactList, numOfRecs); break;
  		case 'O': openFile(contactList, numOfRecs); break;
  		case 'S': saveFile(contactList, numOfRecs); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
  
  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}

void addData(Person list[], int& size)
{
  Person tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << endl;
		cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
		// see if this record should be added to the array
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const Person list[], int size)
{
  system("cls");
  
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;
        
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(10)  << list[i].addr<<left<<endl;
    }
    
    cout << "********************************************" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }
  
  system("PAUSE");
  system("cls");
}

// Save records to disc
void saveFile(const Person list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());
  
  // to make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";
    
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}

// Open file and load array
void openFile(Person list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading Address Book from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
      
      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].addr;
  
    }
    cout << endl << size << " Contacts read from the disc." << endl;

  
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}

char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}
Comments
Next time try the EDIT button. Or before posting preview it with the ADVANCED EDITOR button.

Sorry for being a little messy! I found some other mistakes I made and fixed em up but I still cant get the phone number and address to be displayed.
heres my code so far:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct Person
{
 			string name;
 			string addr;
 			string phone;
 			string email;
				};

const int MAX_SIZE = 8; // max number of contacts

// functin prototypes
void addData(Person list[], int& size);
void dispData(const Person list[], int size); 
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();			 
int main(int argc, char *argv[])
{
  Person contactList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(contactList, numOfRecs); break;
  		case 'D': dispData(contactList, numOfRecs); break;
  		case 'O': openFile(contactList, numOfRecs); break;
  		case 'S': saveFile(contactList, numOfRecs); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
  
  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}

void addData(Person list[], int& size)
{
  Person tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << endl;
		cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.phone = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.email = str;
    cout << endl;
		// see if this record should be added to the array
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const Person list[], int size)
{
  system("cls");
  
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;
        
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(10)  << list[i].addr<<left<<endl;
    }
    
    cout << "********************************************" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }
  
  system("PAUSE");
  system("cls");
}

// Save records to disc
void saveFile(const Person list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());
  
  // to make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";
    
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}

// Open file and load array
void openFile(Person list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading Address Book from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
      
      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].addr;
  
    }
    cout << endl << size << " Contacts read from the disc." << endl;

  
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}

char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}

Try the following version of the display function:

void dispData(const Person list[], int size)
{
    system("cls");

    if(size < 1)
    {
        cout << "Nothing to display" << endl;
    }
    else
    {
        cout << "*******************************************" << endl;

        for (int i = 0; i < size; i++)
        {
            cout << setw(10) << right << "Name: " << setw(32) << left  << list[i].name << endl
                 << setw(10) << right << "Address: " << setw(64) << left << list[i].addr << endl
                 << setw(10) << right << "Phone #: "<< setw(13) << left << list[i].phone
                 << setw(16) << right << "Email: "<< setw(32) << left << list[i].email
                 << endl;
            cout << "********************************************" << endl;
        }

        cout << right << setw(3) << size;
        cout << " Contacts"<< endl;
    }

    system("PAUSE");
    system("cls");
}

Thanks for ur help. I kinda messed it up but got it to compile again. I just need to get it to delete/edit/sort data and I tried really hard but I couldnt figure out how to make that happen. I need help please I have only 45 minutes to finish this. Thank you.

Here is what I have so far but it doesnt seem to be right as the compiler is telling me vector is not declared.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
 
using namespace std;
 
struct Person
{
 			string name;
 			string addr;
 			string phone;
 			string email;
				};
 
const int MAX_SIZE = 8; // max number of contacts
 
// functin prototypes
void addData(Person list[], int& size);
void dispData(const Person list[], int size); 
void delData (const Person list[], int size);
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();			 
int main(int argc, char *argv[])
{
  Person contactList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(contactList, numOfRecs); break;
  		case 'D': dispData(contactList, numOfRecs); break;
  		case 'T': delData(contactList, numOfRecs); break;
			case 'O': openFile(contactList, numOfRecs); break;
  		case 'S': saveFile(contactList, numOfRecs); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
 
  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}
 
void addData(Person list[], int& size)
{
  Person tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << endl;
		cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.phone = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.email = str;
    cout << endl;
		// see if this record should be added to the array
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}
 
void dispData(const Person list[], int size)
{
  system("cls");
 
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;
 
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(10) << list[i].name << right
           << setw(10)  << list[i].addr<<right
           << setw(10) << list[i].phone<<left
           << setw(15) << list[i].email<<left<<endl;
    }
 
    cout << "********************************************" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }
 
  system("PAUSE");
  system("cls");
}
 
void delData(const Person list[],int size) {
		 vector<Person> :: iterator vItr = Contact.begin();
		 while (vItr != Contact.end() )
		 {
		 			 if (vItr->Name == Name)
		 			 {
					 		vItr = Contact.erase (vItr);
					 		break;
							}
							else
							vItr++;
							}
							
// Save records to disc
void saveFile(const Person list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());
 
  // to make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";
 
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}
 
// Open file and load array
void openFile(Person list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;
 
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
 
    system("cls");
    cout << "Reading Address Book from the disc ";
 
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
 
      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].addr;
 
    }
    cout << endl << size << " Contacts read from the disc." << endl;
 
 
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
 
}
 
char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}

Hi Everyone,
I really wanna finish this code and see how it would function. I still did not get it to edit, delete or search for data. I will post my code and I wanna know how can I get these functions to work properly. I really do not know how. I tried reading my textbook but it did not seem to be helpful in those cases at all. Thank you in advance.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector> 
using namespace std;
 
struct Person
{
 			string name;
 			string addr;
 			string phone;
 			string email;
				};
 
const int MAX_SIZE = 8; // max number of contacts
 
// functin prototypes
void addData(Person list[], int& size);
void dispData(const Person list[], int size); 
void findData(const Person list[], int size);
void sortData(const Person list[], int size);
void eraseData(const Person list[], int size);
void saveFile(const Person list[], int size);
void openFile(Person list[], int& size);
char getMenuResponse();			 
int main(int argc, char *argv[])
{
  Person contactList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Address Book - " << numOfRecs << " Number of Contacts" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(contactList, numOfRecs); break;
  		case 'D': dispData(contactList, numOfRecs); break;
  		case 'F': findData(contactList, numOfRecs); break;
			case 'S': sortData(contactList, numOfRecs); break;
			case 'E': eraseData(contactList,numOfRecs); break;
  		case 'O': openFile(contactList, numOfRecs); break;
  		case 'S': saveFile(contactList, numOfRecs); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
 
  // system("PAUSE"); // Commented out so the program exits upon request
  return EXIT_SUCCESS;
}
 
void addData(Person list[], int& size)
{
  Person tmp; // declare a temp contact that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; I am using a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Contact Information" << endl << endl;
    cout << "Name: ";
    
    cin.getline(str, 256, '\n'); 
    tmp.name = str;
    cout << endl;
		cout << "Address: ";
    cin.getline(str, 256, '\n');
    tmp.addr = str;
    cout << endl;
    cout << "Phone Number: ";
    cin.getline(str, 256, '\n');
    tmp.phone = str;
    cout << endl;
    cout << "E-mail Address: ";
    cin.getline(str, 256, '\n');
    tmp.email = str;
    cout << endl;
		
    cout << "Add Contact to Address Book? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Sorry, Address Book is currently full." << endl;
    system("pause");
  }
  system("cls");
}
 
void dispData(const Person list[], int size)
{
  system("cls");
 
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Contacts :" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Contact Name   Address    Phone No.  Email" << endl;
    cout << "*******************************************" << endl;
 
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(10) << list[i].name << right
           << setw(10)  << list[i].addr<<right
           << setw(10) << list[i].phone<<left
           << setw(15) << list[i].email<<left<<endl;
    }
 
    cout << "********************************************" << endl;
    cout << right << setw(3) << size;
    cout << " Contacts"<< endl;
  }
 
  system("PAUSE");
  system("cls");
}
 
void findData(const Person list[], int size);
 

// To save file
void saveFile(const Person list[], int size) {
     string fileName;
  ofstream outfi;
  cout<<"Enter file name: ";
  getline(cin,fileName);
  outfi.open(fileName.c_str());
 

  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving Address Book to the disc ";
 
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].addr<< ';';
      
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " Address Book written to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
}
 

void openFile(Person list[], int& size)
{
  ifstream infi("AddressBook.txt");
  string str;
  stringstream strstrm;
 
  
  if (!infi.fail()) { 
 
    system("cls");
    cout << "Reading Address Book from the disc ";
 
    size = 0; 
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
 
      
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); 
      strstrm << str; 
      strstrm >> list[size].addr;
 
    }
    cout << endl << size << " Contacts read from the disc." << endl;
 
 
    system("PAUSE");
    system("cls");
  }
  else { // Opening file was not successful
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
 
}
 
char getMenuResponse()

{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd contact, (D)isplay contact0, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}

Edited 4 Years Ago by Nawaf15: n/a

Please, be patient; not everything gets an answer right away, that's just the nature of these fora.

Of the three operations, the easiest is searching; however, you would need to have more information passed to the function than just the list of contacts and the number of contacts, you'll need the name you are searching for as well. Something like this for the function prototype:

void findbyName(const Person list[], int size, string name);

For the function itself, the easiest solution for a short, unordered array like this is just a brute-force linear search, using a for() loop through all the items until you reach the last one or you find a match against list.name .

This article has been dead for over six months. Start a new discussion instead.