// input output file example
// 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;
    ofstream my_file; //declare file for output
    
    // get data from user
    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
    {
        my_file.open("name_age.txt", ios::out); //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";

    }

    return 0;
} // main