Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP FAST, it must be C++!!! Write a program that maintains a database containin

ID: 3788720 • Letter: H

Question

HELP FAST, it must be C++!!!

Write a program that maintains a database containing data, such as name and birthday, about your friends and

relatives. You should be able to enter, remove, modify, or search this data. Initially, you can assume that the

names are unique. The program should be able to save the data in a fi le for use later.

Design a class to represent the database and another class to represent the people. Use a binary search tree

of people as a data member of the database class.

You can enhance this problem by adding an operation that lists everyone who satisfi es a given criterion.

For example, you could list people born in a given month. You should also be able to list everyone in the

database.

Explanation / Answer

        // A simple example of Friend's records ...

   // using a C++ 'list' container to hold each 'Friend's' element

  

   // . adds info from keyboard into a list of 'Friend's' records (using push_back)

   // . shows the 'records' in memory on the screen

   // . writes to file ...         all the records in memory

   // . reads from file and stores all the records in memory (in a C++ list)

   // . allows edit/erase ... to a Friend's record

   // . sorts Friend's records by Friend's name ...

   // . sorts Friend's records by Friend's DOB ...

   // Note:

   // * this program allows only unique Friend's name for each record

   // * this program is set to update its file, (if there were any changes),

   //   before quitting

  

   #include <fstream>

   #include <iostream>

   #include <string>

   #include <list>

   #include <cctype>

  

   // Globals ...

   using namespace std;

   const char THIS_TERM[] = "fall.txt"; // file name for this term's records

   const string HEADER =   " What do you want to do ? "

                            " . Add a new friend name and DOB ? "

                            " . Edit/Erase a friend record ? "

                            " . Sort a Friend's record ? "

                            " . View all in memory at present ? "

                            " . U pdate file with data currently in memory ? "

                            " . Load in data from file ? "

                            " . eX it program ? ";

                           

   class Friend

   {

   public:

        // constructors ...

        Friend(){}


       

        // setters ...

        voDOB setName(string nam){ name = nam; }

        voDOB setDOB(string num) { DOB = num; }

  

        // getters ...

        string getName() { return name; }

        string getDOB() { return DOB; }

   private:

        string name, // add any needed info here, just like in a C/C++ 'struct'

               DOB;

   };

  

  

   // functions used by main to process a list of Friend's records

  

   // returns a valDOB iterator if DOB is used already ... otherwise returns NULL

   list< Friend >::iterator existDOB( list< Friend's >& term, string& DOB )

   {

        list< Friend >::iterator it;

        for( it = term.begin(); it != term.end(); ++it )

        {

            if( it->getDOB() == DOB )

                return it;

        }

               

        return NULL;

   }

  

   // adds Friend's records to end of end of list of Friend's records ... 'term'

   // gets input from keyboard ...

   int newFriend( list< Friend >& term )

   {

        cout << " Enter an empty record to exit this 'Input Loop' ..." << endl;

        int count = , reply;

        string nam, num;

        for( ;; ) // loop forever until break ...

        {

            cout << " DOB   : ";

            getline(cin, num);

            cout << "Name : ";

            getline(cin, nam);

            if( nam=="" || num=="")

                break;

  

            cout << "Add or Redo (a/r) ? ";

            reply=cin.get();

            cin.sync();

            if ( toupper(reply)!='A' )

            {

                cout << "Aborted ..." << endl;

                continue;

            }

           

            // ok ... create and add this record to the end of the list ...

            term.push_back( Friend(nam, num) );

            ++count;

            cout << "Added ..." << endl;

        }

        return count;

   }

  

   // shows (to console screen) all Friend's records in list container ... 'term'

   void viewFriend( list< Friend >& term )

   {

        list< Friend >::iterator it;

        int i = ;

        for( it = term.begin(); it != term.end(); ++it )

        {

            cout << ++i << ". "

                 << "Name   : " << it->getName() << endl

                 << "Date of Birth : " << it->getDOB() << endl;

        }

   }

  

   // file all records in memory ... create/overwrite file with name 'THIS_TERM'

   int fileFriend( list< Friend>& term )

   {

        ofstream fout( THIS_TERM ); // recall 'THIS_TERM' is a Global variable

        if ( !fout )

        {

            cout << " Error attempting to open file ..." << endl;

            return -;    // report error condition ...

        }

  

        // else ...

        list< Friend >::iterator it;

        int i = ;

        for( it = term.begin(); it != term.end(); ++it )

        {

            fout << it->getName() << "," << it->getDOB() << endl;

            ++i;

        }

       

        fout.close();

       

        if( i == (int)term.size() )

            cout << " All " << i << " records filed ok." << endl;

        else

            cout << " Only " << i << " of " << term.size()

                 << " records were written ..." << endl;

                

        return i; // report success ... i.e. report count of records filed

   }

  

   // reads in all Friend's records from file 'THIS_TERM' ... if it exists

   // returns - if no file exists; else returns the number of records read

   int readFriend( list< Friend's >& term )

   {

        ifstream fin( THIS_TERM ); // recall THIS_TERM is a Global variable

        if ( !fin )

        {

            cout << "Error attempting to open file ... "

                 << "(Perhaps it dosen't exist yet?)" << endl;

            return -;          // report error condition ...

        }

  

        // else ... check existing term.size() first before re-setting?

        if( term.size() ) // i.e. if not == ...

        {

            cout << " Do you want over-write the " << term.size()

                 << " records in memory (y/n) ? " << flush;

            int reply = toupper( cin.get() );

            cin.sync();

            if( reply != 'Y' )

            {

                cout << "Aborted ... " << flush;

                return ;

            }

        }

       

        // else ... if reach here ...

        list< Friend>temp;

        term = temp; // set term to null ...

        string nam, num;

        int i;

        for( i=; getline( fin, nam, ',' ); ++i ) //first get st string (up to ',')

        {

            getline( fin, num, ' ' ); // then get rest of line (up to ' ')

            term.push_back( Friend(nam, num) ); // construct and add new Friend's

        }

        fin.close();

        return i; // report success? ... i.e. return the record count ...

   }

  

   // returns 'true' is a record was edited or erased; otherwise returns false

   bool editFriend( list< Friend> &term )

   {

        cout << " Enter an empty record to exit this 'Edit/Erase Function' ..."

             << " Enter the DOB of the Friend's record to edit ? " << flush;

        string DOBStr, nam;

        getline( cin, DOBStr );

  

        list< Friend >::iterator i, index;

        i = existDOB( term, DOBStr );

        if( i == NULL )

        {

            cout << "This '" << DOBStr << "' does not exist." << endl;

            return false;

        }

       

        // else ... show ... and ask if ok to edit ...

        cout << "Name   : " << i->getName() << endl

             << "DOB : " << i->getDOB() << endl

            

             << "Ok to edit/erase (y/n) ? " << flush;

            

        int reply = toupper( cin.get() );

        cin.sync();

        if( reply != 'Y' )

        {

            cout << "Aborted ... " << endl;

            return false;

        }

       

        cout << " Do you want to erase this record (y/n) ? " << flush;

        reply = toupper( cin.get() );

        cin.sync();

        if( reply == 'Y' )

        {

            term.erase( i );

            cout << "Erased ..." << endl;

            return true;

        }

           

       

        // else ...

       

        cout << " New DOB   : ";

        getline(cin, DOBStr);

        index = existDOB( term, DOBStr );

        if( index != NULL && index!= i )

        {

            cout << " That " << DOBStr << " already exits ... " << endl;

            return false; // exit to menu now ...

        }

       

        cout << "New Name : ";

        getline(cin, nam);

        if( nam=="" || DOBStr=="")

            return false;

  

        cout << "Ok or Redo (o/r) ? ";

        reply=cin.get();

        cin.sync();

        if( toupper(reply)!='O' ) // cap 'O' ... as in Ok

        {

            cout << "Aborted ..." << endl;

            return false;

        }

  

        // ok ... do edit

        i->setName( nam );

        i->setDOB( DOBStr );

        cout << "Edited ..." << endl;

        return true;

   }

  

  
     

   void pauseForEnter()

   {

        cout << " Press 'Enter' to continue ... " << flush;

        cin.sync();

        cin.get();

   }

  

  

  

   int main()

   {

        // create a 'fall list' to hold Friend's names and DOBs for the 'Fall Term'

        // also holds number of records, via 'fall.size()'

        list <Friend > fall;

  

        // now get all records from file ... if it exists ?

        int count = readFriend( fall );

        if( count >= )

            cout << count << " Friend's record(s) read into memory ..." << endl;

        else

            cout <<"(The file will be created when some Friend's records exist.)"

                 <<endl;

  

        bool changes = false; // set 'update file flag' to initial value ...

        int reply;

        for( ;; ) // loop forever ... until break ...

        {

            cout << HEADER;

            reply = toupper(cin.get());

            cin.sync(); // flush cin stream ...

  

            if( reply == '' || reply == 'A' )

            {

                int newFriend = newFriend( fall );

                cout << endl << newFriend << " Friend's record(s) added ..."

                     << " The total number of Friend's records now is "

                     << fall.size() << endl;

                if( newFriend )

                    changes = true; // if > update bool variable changes

            }

            else if( reply == '' || reply == 'E' )

            {

                if( editFriend( fall ) )

                    changes = true;

            }

            else if( reply == '' || reply == 'S' )

            {

                cout << " Sort by DOB? " << flush;

                reply = toupper( cin.get() );

                cin.sync();

                cout << "Now sorted in memory by ";

                if( reply == 'I' )

                {

                    fall.sort(compare_DOB);

                    cout << "DOB. ";

                }

              
                cout << "Update file (y/n) ? " << flush;

                reply = toupper( cin.get() );

                cin.sync();

                if( reply == 'Y' )

                {

                    changes = true;

                    cout << "File to be updated ... ";

                }

            }

            else if( reply == '' || reply == 'V' )

                newFriend( fall );

            else if( reply == '' || reply == 'U' )

            {

                if( !changes )

                    cout << " No changes to file ..." << endl;

                else

                {

                    cout << "Are you sure you want to update the file (y/n) ? "

                         << flush;

                    reply = toupper( cin.get() );

                    cin.sync();

                    if( reply == 'Y' )

                    {

                        if( fileFriend( fall ) != (int)fall.size() )

                            cout << " ERROR! NOT all records were filed!" << endl;

                        else

                        {

                            changes = false;// update file flag ...

                            cout << "File write operation confirmed." << endl;

                        }

                    }

                }

            }

            else if( reply == '' || reply == 'L' )

            {

                int condition = readFriend( fall );

                if( condition >= )

                    cout << " There were " << condition

                         << " Friend's records read into memory from file." << endl;

            }

            else if( reply == '' || reply == 'X' )

            {

                if( changes ) // then ...

                    fileFriend( fall );

                break; // and exit program ...

            }

            else

            {

                cout << " This choice not implemented yet ... " << endl;

            } // end of if( reply == ? ) switch structure ...

  

        } // end of forever loop ...

  

        // if you have a Win OS ... show structure of file

        string tmpSee = " ";

        tmpSee = "notepad" + tmpSee + THIS_TERM;

        system( tmpSee.c_str() );

   }