C++ search, delete/undelete, update functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: C++ search, delete/undelete, update functions

  1. #1
    Join Date
    Sep 2012
    Posts
    3

    C++ search, delete/undelete, update functions

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #include <fstream>
    
    using namespace std;
    
    const char FILE_PATH[] = "E:\\iofile";
    //------------------------------------------------------------------------------
    
    struct Student
    {
    *** char names [ 16 ];
    *** int other;
    };
    
    fstream MyFile ( FILE_PATH , ios :: binary | ios :: in | ios :: out );
    
    void createFile();
    void mainMenu();
    void funcSwitch();
    void addRecord();
    int displayRecord();
    void updateRecord();
    void deleteRecord();
    void printRecords();
    int searchRecord();
    //------------------------------------------------------------------------------
    int main ( int argc, char* argv [] )
    {
    *** createFile();
    
    *** mainMenu();
    }
    //------------------------------------------------------------------------------
    void mainMenu()
    {
    *** cout << "Welcome to School Enrolment System" << endl << endl
    *** *** * << "Please enter your selection: " << endl
    *** *** * << "1.** Register - New Student (Add record)" << endl
    *** *** * << "2.** Look Up - Existing Student (Display record)" << endl
    *** *** * << "3.** Change Details (Update record)" << endl
    *** *** * << "4.** Remove Student (Delete record)" << endl
    *** *** * << "5.** Show all Students (Print all)" << endl
    *** *** * << "6.** Exit " << endl << endl
    *** *** * << "Selection: ";
    
    *** funcSwitch();
    }
    //------------------------------------------------------------------------------
    void funcSwitch()
    {
    ** char choice;
    
    *** choice = getch();
    *** clrscr();
    *** while ( choice != 54 )
    ** {
    ***** switch ( choice )
    *** *** {
    ******** case '1' :
    *********** addRecord();
    *********** getch();
    *** *** *** *** clrscr();
    ************** break;
    
    ******** case '2' :
    *********** displayRecord();
    *********** getch();
    ************** break;
    
    ******** case '3' :
    *********** updateRecord();
    *********** getch();
    ************** break;
    
    ******** case '4' :
    *********** deleteRecord();
    *********** getch();
    ************** break;
    
    ******** case '5' :
    *********** printRecords();
    *********** getch();
    ************** break;
    
    ******** case '6' :
    *********** break;
    
    ******** default :
    *** *** *** *** cout << choice << " is not a vaild choice" << endl << endl
    *** *** *** *** *** * << "Press any key to choose again... ";
    *********** getch();
    *** *** *** *** clrscr();
    
    ***** }
    ***** mainMenu();
    *** *** choice = getch();
    *** *** clrscr();
    ** }
    }
    //------------------------------------------------------------------------------
    void addRecord()
    {
    *** Student temp;
    
    *** cout << "Welcome new Student!" << endl
    *** *** * << "We're going to set you up with a new Enrolment..." << endl
    *** *** * << "All we need is a few details, let's begin: " << endl;
    
    *** cout << endl << "Please enter first name : ";
    *** cin >> temp.names;
    
    
    
    *** cout << endl << "Please enter your age : ";
    *** cin >> temp.other;
    
    
    *** MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
    *** MyFile.clear();
    *** MyFile.write ( ( const char* ) &temp, sizeof ( Student ) );
    *** MyFile.clear();
    *** MyFile.close();
    }
    //------------------------------------------------------------------------------
    int searchRecord()
    {
    
    *** return 0;
    }
    //------------------------------------------------------------------------------
    int displayRecord()
    {
    *** Student temp;
    *** int age;
    *** int position;
    
    *** cout << "Enter age of person you are seeking: ";
    *** cin >> age;
    *** clrscr();
    
    
    
    *** MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
    *** MyFile.clear();
    *** MyFile.seekp ( position * sizeof ( Student ), ios :: beg );
    *** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
    
    *** if ( age != temp.other )
    *** {
    *** *** cout << endl << "No record found. Press any key to continue...";
    *** }
    *** else
    *** {
    *** *** cout << "We found your record: " << endl << endl
    *** *** *** * << "Name: " << temp.names << endl
    *** *** *** * << "Age:* " << temp.other << endl;
    *** }
    
    *** *** *cout << endl << "Press any key to continue..." << endl;
    *** *** *getch();
    *** *** *clrscr();
    *** *** *mainMenu();
    *** *** *MyFile.close();
    *** *** *return 0;
    
    }
    //------------------------------------------------------------------------------
    void updateRecord()
    {
    *** Student temp;
    *** time_t t;
    *** char choice;
    *** int position;
    
    *** position = displayRecord ();
    
    *** MyFile.open( FILE_PATH, ios :: binary | ios :: out );
    *** MyFile.clear();
    
    *** if( position != -1 )
    *** {
    *** *** cout << endl << "Do you want to Update this Record( y or n )";
    *** *** choice = getch();
    *** }
    
    *** if( choice == 'y' )
    *** {
    *** *** cout << endl << "Enter name : ";
    *** *** cin >> temp.names;
    
    *** *** cout << endl << "And your age : ";
    *** *** cin >> temp.other;
    
    
    
    
    *** *** MyFile.open( FILE_PATH, ios :: binary | ios :: out );
    *** *** MyFile.clear();
    *** *** MyFile.seekp( position * sizeof( Student ), ios :: beg );
    ***** MyFile.clear();
    *** *** MyFile.write( ( const char* ) &temp, sizeof( Student ) );
    ***** MyFile.close();
    *** }
    }
    
    
    //------------------------------------------------------------------------------
    void deleteRecord()
    {
    
    *** Student temp;
    *** char choice;
    *** int position;
    
    *** -MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
    *** MyFile.clear();
    *** MyFile.seekp( position * sizeof( Student ), ios :: beg );
    *** MyFile.read( ( char* )&temp, sizeof( Student ) );
    
    *** cout << endl << endl;
    *** cout << "Would you like to delete this record? Yes (y) or No (n): ";
    *** cin >> choice;
    
    *** choice = getch();
    
    *** MyFile.seekp( position * sizeof( Student ), ios :: beg );
    *** MyFile.write( ( const char* )&temp, sizeof( Student ) );
    
    *** getch();
    *** clrscr();
    *** mainMenu();
    
    *** MyFile.close();
    }
    
    //-----------------------------------------------------------------------------
    
    void printRecords()
    {
    *** Student temp;
    
    *** MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
    *** MyFile.clear();
    *** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
    
    *** while ( ! MyFile.eof() )
    *** {
    *** *** cout << "Name: " << temp.names << endl
    *** *** *** * << "Age:* " << temp.other << endl << endl;
    
    
    *** *** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
    *** }
    
    *** cout << endl << "Press any key to continue back to main menu" << endl;
    *** getch();
    *** clrscr();
    *** mainMenu();
    
    *** MyFile.close();
    }
    //------------------------------------------------------------------------------
    void createFile()
    {
    *** if( ! MyFile )
    *** {
    *** *** MyFile.close();
    *** *** MyFile.open(FILE_PATH, ios::binary | ios::in | ios::out);
    *** }
    
    *** if ( ! MyFile )
    *** {
    *** *** cout << "File Error: File does not exist. Creating new file..."
    *** *** *** * << endl << "Click to continue to main menu...";
    *** *** getch();
    *** *** clrscr();
    *** }
    }
    Basically the program is a database of student enrollments.

    I cannot get the the search delete/undelete and update functions to work and need help asap. Need to implement a bool field to the delete / undelete

    Please help

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: C++ search, delete/undelete, update functions

    First, indent your code properly. This will make your code more readable, helping you to trace the flow of control through your program.

    Then, you should tell us exactly how do the functions fail to work.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2012
    Posts
    3

    Re: C++ search, delete/undelete, update functions

    Code:
    //------------------------------------------------------------------------------
    
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    #include <fstream>
    
    using namespace std;
    
    const char FILE_PATH[] = "E:\\iofile";
    //------------------------------------------------------------------------------
    
    struct Student
    {
        char names [ 16 ];
        int other;
    };
    
    fstream MyFile ( FILE_PATH , ios :: binary | ios :: in | ios :: out );
    
    void createFile();
    void mainMenu();
    void funcSwitch();
    void addRecord();
    int displayRecord();
    void updateRecord();
    void deleteRecord();
    void printRecords();
    int searchRecord();
    //------------------------------------------------------------------------------
    int main ( int argc, char* argv [] )
    {
        createFile();
    
        mainMenu();
    }
    //------------------------------------------------------------------------------
    void mainMenu()
    {
        cout << "Welcome to School Enrolment System" << endl << endl
              << "Please enter your selection: " << endl
              << "1.   Register - New Student (Add record)" << endl
              << "2.   Look Up - Existing Student (Display record)" << endl
              << "3.   Change Details (Update record)" << endl
              << "4.   Remove Student (Delete record)" << endl
              << "5.   Show all Students (Print all)" << endl
              << "6.   Exit " << endl << endl
              << "Selection: ";
    
        funcSwitch();
    }
    //------------------------------------------------------------------------------
    void funcSwitch()
    {
       char choice;
    
        choice = getch();
        clrscr();
        while ( choice != 54 )
       {
          switch ( choice )
            {
             case '1' :
                addRecord();
                getch();
                    clrscr();
                   break;
    
             case '2' :
                displayRecord();
                getch();
                   break;
    
             case '3' :
                updateRecord();
                getch();
                   break;
    
             case '4' :
                deleteRecord();
                getch();
                   break;
    
             case '5' :
                printRecords();
                getch();
                   break;
    
             case '6' :
                break;
    
             default :
                    cout << choice << " is not a vaild choice" << endl << endl
                          << "Press any key to choose again... ";
                getch();
                    clrscr();
    
          }
          mainMenu();
            choice = getch();
            clrscr();
       }
    }
    //------------------------------------------------------------------------------
    void addRecord()
    {
        Student temp;
    
        cout << "Welcome new Student!" << endl
              << "We're going to set you up with a new Enrolment..." << endl
              << "All we need is a few details, let's begin: " << endl;
    
        cout << endl << "Please enter first name : ";
        cin >> temp.names;
    
    
    
        cout << endl << "Please enter your age : ";
        cin >> temp.other;
    
    
        MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
        MyFile.clear();
        MyFile.write ( ( const char* ) &temp, sizeof ( Student ) );
        MyFile.clear();
        MyFile.close();
    }
    //------------------------------------------------------------------------------
    int searchRecord()
    {
    
        return 0;
    }
    //------------------------------------------------------------------------------
    int displayRecord()
    {
        Student temp;
        int age;
        int position;
    
        cout << "Enter age of person you are seeking: ";
        cin >> age;
        clrscr();
    
    
    
        MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
        MyFile.clear();
        MyFile.seekp ( position * sizeof ( Student ), ios :: beg );
        MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
    
        if ( age != temp.other )
        {
            cout << endl << "No record found. Press any key to continue...";
        }
        else
        {
            cout << "We found your record: " << endl << endl
                  << "Name: " << temp.names << endl
                  << "Age:  " << temp.other << endl;
        }
    
             cout << endl << "Press any key to continue..." << endl;
             getch();
             clrscr();
             mainMenu();
             MyFile.close();
             return 0;
    
    }
    //------------------------------------------------------------------------------
    void updateRecord()
    {
        Student temp;
        time_t t;
        char choice;
        int position;
    
        position = displayRecord ();
    
        MyFile.open( FILE_PATH, ios :: binary | ios :: out );
        MyFile.clear();
    
        if( position != -1 )
        {
            cout << endl << "Do you want to Update this Record( y or n )";
            choice = getch();
        }
    
        if( choice == 'y' )
        {
            cout << endl << "Enter name : ";
            cin >> temp.names;
    
            cout << endl << "And your age : ";
            cin >> temp.other;
    
    
    
    
            MyFile.open( FILE_PATH, ios :: binary | ios :: out );
            MyFile.clear();
            MyFile.seekp( position * sizeof( Student ), ios :: beg );
          MyFile.clear();
            MyFile.write( ( const char* ) &temp, sizeof( Student ) );
          MyFile.close();
        }
    }
    
    
    //------------------------------------------------------------------------------
    void deleteRecord()
    {
    
        Student temp;
        char choice;
        int position;
    
        MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
        MyFile.clear();
        MyFile.seekp( position * sizeof( Student ), ios :: beg );
        MyFile.read( ( char* )&temp, sizeof( Student ) );
    
        cout << endl << endl;
        cout << "Would you like to delete this record? Yes (y) or No (n): ";
        cin >> choice;
    
        choice = getch();
    
        MyFile.seekp( position * sizeof( Student ), ios :: beg );
        MyFile.write( ( const char* )&temp, sizeof( Student ) );
    
        getch();
        clrscr();
        mainMenu();
    
        MyFile.close();
    }
    
    //-----------------------------------------------------------------------------
    
    void printRecords()
    {
        Student temp;
    
        MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
        MyFile.clear();
        MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
    
        while ( ! MyFile.eof() )
        {
            cout << "Name: " << temp.names << endl
                  << "Age:  " << temp.other << endl << endl;
    
    
            MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
        }
    
        cout << endl << "Press any key to continue back to main menu" << endl;
        getch();
        clrscr();
        mainMenu();
    
        MyFile.close();
    }
    //------------------------------------------------------------------------------
    void createFile()
    {
        if( ! MyFile )
        {
            MyFile.close();
            MyFile.open(FILE_PATH, ios::binary | ios::in | ios::out);
        }
    
        if ( ! MyFile )
        {
            cout << "File Error: File does not exist. Creating new file..."
                  << endl << "Click to continue to main menu...";
            getch();
            clrscr();
        }
    }
    //------------------------------------------------------------------------------
    Last edited by stevensonnz; September 24th, 2012 at 07:40 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,424

    Re: C++ search, delete/undelete, update functions

    Quote Originally Posted by stevensonnz View Post
    I cannot get the the search delete/undelete and update functions to work
    If you wrote the program, then you had some sort of logic in mind to get the program to work. If that logic fails, then it's your job to debug your code using the debugger, and seeing where the flow of the program fails to follow the logic you had in mind. Then you fix the code so that the logic follows your original plans.

    The bottom line is that you have to put the effort in debugging your own code -- if something doesn't work, then identify for yourself exactly what is not working. If you have done that and you still are having trouble, then tell us what line of code, what library function, etc. is doing something that you didn't expect. Posting code verbatim and basically asking "go debug this for me" isn't (or shouldn't be) the way it's done here.

    Having said that, one thing that doesn't make sense:
    Code:
     MyFile.open( FILE_PATH, ios :: binary | ios :: out );
    How can you update a record if you erase the file? Opening a file for output erases the original file. If you debugged your code, you would see that this will not work. From there, you would then go to your book and see how to open a file for r/w access, and change the code accordingly.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 24th, 2012 at 04:16 AM.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,354

    Re: C++ search, delete/undelete, update functions

    Your code does not compile. So how could do something like
    Quote Originally Posted by stevensonnz
    I cannot get the the search delete/undelete and update functions to work and need help asap. Need to implement a bool field to the delete / undelete
    Victor Nijegorodov

  6. #6
    Join Date
    Sep 2012
    Posts
    3

    Re: C++ search, delete/undelete, update functions

    Try now it should compile

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center