// input output file example
// appending / writing data to files
// Barb Sanchez 302 alternate assignments
// 9-10-02

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string first, last;
    int age;
    char again ='Y';
    ofstream my_file; //declare file for output
    
    // get data from user

    while (again =='Y' || again =='y')
    {
        cout << "\nEnter your first name";
        cin >> first;

        cout << "\nEnter your last name";
        cin >> last;

        cout << "\nHow old are you?";
        cin >> age;

        if (my_file) //if no error occurs when opening file
        {
            //append data to existing file
            my_file.open("name_age.txt", ios::app); //give file name
            my_file << first << endl;
            my_file << last << endl;
            my_file << age << endl;
            my_file.close(); //close data file after writing
        }
    
            else   // if an error, output error message
            {
                cout << "\nAn error occurred while opening the file\n";

            }

        cout << "\nDo you want to input another name?";
        cin >> again;
    } //while

    return 0;
} // main