How to write a C program to generate a file containing at le
How to write a C++ program to generate a file containing at least a 1000 entries in the following format (each line represents one record): For Medical Personnel: FirstName LastName ID Role (MD or NP) DutyDays (3 Initials for the days in a week) and For Patients Personnel: FirstName LastName ID Role ExitFlag (Y or N) AdmitDate (MM:DD:YYYY) OptionalExitDate (MM:DD:YYYY) - If the ExitFlag is Y (Yes) this means there exists an OptionalExitDate
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
 #include <fstream>
 #include <iomanip>
using namespace std;
int main() // driver method
 {
    ofstream medical, patients; // required initialisations
    string firstname, lastname, role, dutydays, exitflag, admitdate, optionalexitdate;
    int id;
   
    medical.open (\"Medical Personnel.txt\"); // opening the text file
    medical << \"FirstName\" << setw(15) << \"LastName\" << setw(15) << \"ID\" << setw(15) << \"Role\" << setw(15) << \"DutyDays\"; // set the headers
   
    cout << \"Enter the Data For the Medical Personnel Text File.\" << endl; // prompt for the user about the code
   
    for(int i = 0; i < 2; i++) { // getting two details from the user
        cout << \"Enter the First Name : \";
        cin >> firstname;
       
        cout << \"Enter the Last Name : \";
        cin >> lastname;
       
        cout << \"Enter the ID : \";
        cin >> id;
       
        cout << \"Enter the Role (MD or NP) : \";
        cin >> role;
       
        cout << \"Enter the Duty Days (3 Initials for the days in a week) : \";
        cin >> dutydays;
       
        medical << \"\ \" << firstname << setw(15) << lastname << setw(15) << id << setw(15) << role << setw(15) << dutydays // write the data to file
    }
    medical.close(); // closing the file
   
    patients.open(\"Patients Personnel.txt\"); // open the file
    patients << \"FirstName\" << setw(15) << \"LastName\" << setw(15) << \"ID\" << setw(15) << \"Role\" << setw(15) << \"ExitFlag\" << setw(15) << \"AdmitDate\" << setw(20) << \"OptionalExitDate\" ; // set the headers
   
    cout << \"Enter the Data For the Patients Personnel Text File.\" << endl; // prompt the user about the data
   
    for(int j = 0; j < 2; j++) { // iterate over the two data from the users
        cout << \"Enter the First Name : \";
        cin >> firstname;
       
        cout << \"Enter the Last Name : \";
        cin >> lastname;
       
        cout << \"Enter the ID : \";
        cin >> id;
       
        cout << \"Enter the Role : \";
        cin >> role;
       
        cout << \"Enter the ExitFlag Value (Y or N) : \";
        cin >> exitflag;
       
        cout << \"Enter the Date of Admit (MM:DD:YYYY) : \";
        cin >> admitdate;
       
        if(exitflag == \"Y\") {
            cout << \"Enter the Optional Exit Date (MM:DD:YYYY) : \";
            cin >> optionalexitdate;
        }
       
        patients << \"\ \" << firstname << setw(15) << lastname << setw(15) << id << setw(15) << role << setw(15) << exitflag << setw(15) << admitdate << setw(15) << optionalexitdate; // write the data to file
       
    }
    return 0;
 }
 OUTPUT :
Enter the Data For the Medical Personnel Text File.
 Enter the First Name : John
 Enter the Last Name : Carter
 Enter the ID : 1234
 Enter the Role (MD or NP) : MD
 Enter the Duty Days (3 Initials for the days in a week) : STM
 Enter the First Name : Brace
 Enter the Last Name : Wein
 Enter the ID : 4567
 Enter the Role (MD or NP) : NP
 Enter the Duty Days (3 Initials for the days in a week) : ThFSa
 Enter the Data For the Patients Personnel Text File.
 Enter the First Name : James
 Enter the Last Name : Joe
 Enter the ID : 789
 Enter the Role : Patient
 Enter the ExitFlag Value (Y or N) : N
 Enter the Date of Admit (MM:DD:YYYY) : 15:06:2015
 Enter the First Name : Vin
 Enter the Last Name : Moore
 Enter the ID : 753
 Enter the Role : Patient
 Enter the ExitFlag Value (Y or N) : Y
 Enter the Date of Admit (MM:DD:YYYY) : 04:05:2015
 Enter the Optional Exit Date (MM:DD:YYYY) : 08:06:2015
 Medical Personnel.txt :
FirstName       LastName             ID           Role       DutyDays
 John         Carter           1234             MD            STM
 Brace           Wein           4567             NP          ThFSa
 Patients Personnel.txt :
FirstName       LastName             ID           Role       ExitFlag      AdmitDate   OptionalExitDate
 James            Joe            789        Patient              N     15:06:2015
 Vin          Moore            753        Patient              Y     04:05:2015     08:06:2015
Hope this is helpful.



