C++ Adding Fstream to code to create, read, and write data to files within code

Viewed 21

I need help on how to create a folder that can read and write the data within my code. I don't understand how to do it using my functions and classes. I understand how to make simple folders within the main function, but trying to do them across functions has me confused. I attempted to start the file functionality but can't seem to get it to work within the doctor file and doctor menu function.

#include <iostream>

#include

#include "Header.h"

#include

using namespace std;

doctor doctorArray[10];


patient patientArray[100];

appointment appointmentArray[1000];

ofstream doctorFile("doctor.txt");
ofstream patientFile("patient.txt");
ofstream appointmentFile("appointment.txt");

int iPat = 0;
int iDoc = 0;
int iApt = 0;

void DrawDoctorFile()
{
    ifstream doctorFile("doctor.txt");
    if (doctorFile.is_open())
    {
        doctor doctorArray;

        for (int i = 0; i < 10; ++i)
        {
            doctorFile >> doctorArray[i];
        }
    }
}

void DrawDoctorsMenu()
{
    string dname;
    string dId;

    int choice;
    bool found = false;


        do

        {

            cout << "Doctors Management\n";

            cout << "======================\n";

            cout << "Please make a selection\n\n";

            cout << "1 - Create a Doctor\n";

            cout << "2 - Retreive existing Doctors\n";

            cout << "3 - Update existing Doctor\n";

            cout << "4 - Delete existing Doctor\n";

            cout << "5 - Exit the Doctors Management\n";

            cout << "Enter your choice: ";

            cin >> choice;

            switch (choice)

            {

            case 1:

                //Create
                cout << "Please input Doctors's Name \n";
                cin >> dname;

                doctorArray[iDoc].setdoctorName(dname);

                cout << "Please input Doctor's ID \n";
                cin >> dId;

                doctorArray[iDoc++].setdoctorID(dId);

             


                break;

            case 2:
                //Retrieve
                cout << "Please enter Doctor's ID \n";
                cin >> dId;
                for (int i = 0; i < iDoc; i++)
                {
                    if (doctorArray[i].getdoctorID() == dId)
                    {
                        cout << "ID = " << doctorArray[i].getdoctorID() << endl;
                        cout << "Name = " << doctorArray[i].getdoctorName() << endl;

                    }

                }

                break;

            case 3:

                //Update
                cout << "Please enter Doctor's ID \n";
                cin >> dId;
                for (int i = 0; i < iDoc; i++)
                {
                    if (doctorArray[i].getdoctorID() == dId)
                    {
                        cout << "Please enter Doctor's updated name \n";
                        cin >> dname;
                        doctorArray[i].setdoctorName(dname);
                        cout << "Updated\n";
                    }
                }



                break;

            case 4:

                //Delete
                cout << "Please enter Doctor's ID \n";
                cin >> dId;

                for (int i = 0; i < iDoc; i++)
                {
                    if (doctorArray[i].getdoctorID() == dId || found)
                    {
                        doctorArray[i] = doctorArray[i + 1];
                        found = true;

                        cout << "Deleted" << endl;
                    }

                }
                found = false;
                break;

            case 5: break;

            default:

                cout << "You made an illegal choice.\n";

            }

        } while (choice != 5);
        DrawDoctorFile();
}
void DrawPatientsMenu()
{

    string pname;
    string pid;
    int choice;
    bool found = false;


    do {

        cout << "Patients Management\n";

        cout << "======================\n";

        cout << "Please make a selection\n\n";

        cout << "1 - Create a Patient\n";

        cout << "2 - Retreive existing Patients\n";

        cout << "3 - Update existing Patient\n";

        cout << "4 - Delete existing Patient\n";

        cout << "5 - Exit the Patient Management\n";

        cout << "Enter your choice: ";

        cin >> choice;

        switch (choice)

        {

        case 1:

            //Create

            cout << "Please input Patient's Name \n";
            cin >> pname;

            patientArray[iPat].setpatientName(pname);

            cout << "Please input Patient's ID \n";
            cin >> pid;

            patientArray[iPat++].setpatientID(pid);


            break;

        case 2:
            //Retrieve
            cout << "Please enter Patient's ID \n";
            cin >> pid;
            for(int i = 0;i<iPat;i++)
            {
                if (patientArray[i].getpatientID() == pid)
                {
                    cout << "ID = " << patientArray[i].getpatientID() << endl;
                    cout << "Name = " << patientArray[i].getpatientName() << endl;

                }
            
            }
            break;

        case 3:

            //Update
            cout << "Please enter Patient's ID \n";
            cin >> pid;
            for (int i = 0; i < iPat; i++)
            {
                if (patientArray[i].getpatientID() == pid)
                {
                    cout << "Please enter Patient's updated name \n";
                    cin >> pname;
                    patientArray[i].setpatientName(pname);
                    cout << "Updated\n";
                }
            }

            break;

        case 4:

            //Delete
            cout << "Please enter Patient's ID \n";
            cin >> pid;

            for (int i = 0; i < iPat; i++)
            {
                if (patientArray[i].getpatientID() == pid || found)
                {
                    patientArray[i] = patientArray[i + 1];
                    found = true;
                    cout << "Deleted" << endl;
                }

            }
            found = false;

            break;

        case 5: break;

        default:

            cout << "You made an illegal choice.\n";

        }

    } while (choice != 5);
};
void DrawApptMenu()
{

    int choice;
    string date;
    string time;
    string dId;
    string pId;
    string apptId;
    bool found = false;

    do {

        cout << "Appointment Management\n";

        cout << "======================\n";

        cout << "Please make a selection\n\n";

        cout << "1 - Create an Appointment\n";

        cout << "2 - Retreive existing Appointment\n";

        cout << "3 - Update existing Appointment\n";

        cout << "4 - Delete existing Appointment\n";

        cout << "5 - Exit the Appointment Management\n";

        cout << "Enter your choice: ";

        cin >> choice;

        switch (choice)

        {

        case 1:

            //Create
            cout << "Please input appointment date: \n";
            cin >> date;

            appointmentArray[iApt].setapptDate(date);

            cout << "Please input appointment time: \n";
            cin >> time;

            appointmentArray[iApt].setapptTime(time);

            cout << "Please use Doctor's ID to assign requested doctor to appointment:\n";
            cin >> dId;

            appointmentArray[iApt].setdoctorID(dId);

            cout << "Please input patient's ID:\n";
            cin >> pId;

            appointmentArray[iApt].setpatientID(pId);

            cout << "Please assign a unique appointment ID: \n";
            cin >> apptId;

            appointmentArray[iApt++].setappointmentID(apptId);

            cout << "Appointment created! \n";



            break;

        case 2:

            //Retreive
            cout << "Please enter appointment ID: \n";
            cin >> apptId;

            for (int i = 0; i < iApt; i++)
            {
                if (appointmentArray[i].getappointmentID() == apptId)
                {
                    cout << "Date = " << appointmentArray[i].getapptDate() << endl;
                    cout << "Time =  " << appointmentArray[i].getapptTime() << endl;
                    cout << "Doctor = " << appointmentArray[i].getdoctorID() << endl;
                    cout << "Patient =  " << appointmentArray[i].getpatientID() << endl;

                }

            }



            break;

        case 3:

            //Update

            cout << "Please enter appointment ID: \n";
            cin >> apptId;

            for (int i = 0; i < iApt; i++)
            {
                if (appointmentArray[i].getappointmentID() == apptId)
                {
                    cout << "Please enter updated time: \n";
                    cin >> time;
                    appointmentArray[i].setapptTime(time);
                    cout << "Updated\n";

                    cout << "Please enter updated date: \n";
                    cin >> date;
                    appointmentArray[i].setapptDate(date);
                    cout << "Updated\n";

                }
            }



            break;

        case 4:

            //Delete
            cout << "Please enter appointment ID \n";
            cin >> apptId;

            for (int i = 0; i < iApt; i++)
            {
                if (appointmentArray[i].getappointmentID() == apptId || found)
                {
                    appointmentArray[i] = appointmentArray[i + 1];
                    found = true;

                    cout << "Deleted" << endl;
                }

            }

            break;

        case 5: break;

        default:

            cout << "You made an illegal choice.\n";

        }

    } while (choice != 5);
};
void DrawMainMenu()
{

    int choice;

    do {

        cout << "Please make a selection\n\n";

        cout << "1 - Doctors Management\n";

        cout << "2 - Patient Management\n";

        cout << "3 - Appointment Management\n";

        cout << "4 - Exit\n";

        cout << "Enter your choice: ";

        cin >> choice;

        switch (choice)

        {

        case 1:

            //Add the Doctors CRUD menu

            DrawDoctorsMenu();

            break;

        case 2:

            //Add the Patients CRUD menu

            DrawPatientsMenu();

            break;

        case 3:

            //Add the Appointments CRUD menu

            DrawApptMenu();

            break;

        case 4: break;

        default:

            cout << "You made an illegal choice.\n";

        }

    } while (choice != 4);
}


int main()
{

    
    DrawMainMenu();

    return 0;
};
0 Answers
Related