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


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

using namespace std;

void addRecord();
void readRecord();
void outputRecord(const char*, const char*, const char*);
int menu();

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

    choice = menu();

    do
    {
        switch (choice)
        {
            case 1:
                while (again == 'Y' || again == 'y') //add data to file
                {
                    addRecord();
                    cout << "Enter another contact: (Y/y)";
                    cin >> again;
                } //while

                break;

            case 2:
                        //reading from the file....
                cout << "Reading data file file....";
                readRecord();
                break;

            case 3:
                exit(1);
        }
    
    choice = menu();

    } while (choice ==1 || choice ==2);




    return 0;

} //main

/***************************************************************************
addRecord()
open data file in append mode, add new contacts to end of list
****************************************************************************/

void addRecord()
{

        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
        }

        
        char fname[20], lname[20], email[30];
        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
        outfile.close();

}
////////////////////////////////////////////////////////////////////////////////


/***************************************************************************
readRecord()
open data file and read in contacts 
****************************************************************************/


void readRecord()
{

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

    cout << endl;
    cout << setw(15)<<"First name" << setw(25) << "Last Name" <<setw(35) << "Email\n " ; 

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

} 
////////////////////////////////////////////////////////////////////////////////



/***************************************************************************
outputRecord()
display data to screen
****************************************************************************/
void outputRecord(const char *fname, const char *lname, const char *email)
{
    cout << setw(15) << fname << setw(25) << lname << setw(35) << email << endl;

}
////////////////////////////////////////////////////////////////////////////////



/***************************************************************************
menu()
display menu to screen, return choice
****************************************************************************/
int menu()
{
    int choice;

    cout << "\n************Menu**************\n";
    cout << " \t (1) Add Data\n";
    cout << " \t (2) Read Data\n";
    cout << " \t (3) Exit\n";
    cout << "\n******************************\n";

    cin >> choice;
    return choice;
    
} 
////////////////////////////////////////////////////////////////////////////////

/*

************Menu**************
         (1) Add Data
         (2) Read Data
         (3) Exit

******************************
1
Enter the first name of your contact: Barb
Enter the last name of your contact: Sanchez
Enter the email address of your contact: barb@barbsanchez.com
Enter another contact: (Y/y)y
Enter the first name of your contact: Smith
Enter the last name of your contact: John
Enter the email address of your contact: jsmith@hotmail.com
Enter another contact: (Y/y)y
Enter the first name of your contact: Clooney
Enter the last name of your contact: Rosemary
Enter the email address of your contact: sing@mp3.com
Enter another contact: (Y/y)n

************Menu**************
         (1) Add Data
         (2) Read Data
         (3) Exit

******************************
2
Reading data file file....
     First name                Last Name                            Email
          Sandy                  Johnson                        sldkfjsdflk
          betty                     boop                       sldkrjsdkflj
           john                    jones                        sdf.234.com
        sanchez                    again                                sdf
           jane                      doe                             lsdkfj
            213                      321                                321
           Barb                  Sanchez               barb@barbsanchez.com
          Smith                     John                 jsmith@hotmail.com
        Clooney                 Rosemary                       sing@mp3.com

************Menu**************
         (1) Add Data
         (2) Read Data
         (3) Exit

******************************
3
Press any key to continue
*/