//email address list
//create data, add to file, read from file


#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip> //for display

using namespace std;

void outputRecord(const char*, const char*, const char*);

int main()
{
    char fname[20], lname[20], email[30];
    char again = 'Y';

    ofstream outfile ("email.dat", ios::app);  //create data file, able to append...



    if (!outfile) //if no file created
    {
        cout << "Error creating file....";
        exit(1); //call exit from std library
    }

    while (again == 'Y' || again == 'y')
    {

        cout << "Enter the first name of your contact: ";
        cin >> fname;

        cout << "Enter the last name of your contact: ";
        cin  >> lname;

        cout << "Enter the email address of your contact: ";
        cin >> email;

        outfile << fname << "  " << lname << "  " << email << "\n"; //put data in file
        
        cout << "Enter another contact: (Y/y)";
        cin >> again;

    } //while

    outfile.close();

    //reading from the file....
    cout << "Reading data file file....";

    ifstream infile ("email.dat", ios::in); //for reading from file
    
    if (!infile)
    { 
        cout << "Error opening file...";
        exit (1);
    }

    cout << "\nFirst name" << setw(25) << "Last Name" <<setw(35) << "Email\n " ; 
    cout << "\n**********" << setw(25) << "*********" <<setw(35) << "************\n " ;

    while (infile >> fname >> lname >> email) //while not at end of file....
    {
        outputRecord(fname, lname, email);
    }
    return 0;

} //main


void outputRecord(const char *fname, const char *lname, const char *email)
{
    cout << setw(10) << fname << setw(20) << lname << setw(35) << email << endl;

}