To Display Sequential Records of non-fixed length

Discussion in 'C++' started by cherries, Dec 20, 2007.

  1. cherries

    cherries New Member

    Joined:
    Dec 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi I am a newbie in C++. I need some help here for my homework.

    I need to design a staff information enquiry program which can display information of staff sequentially in staff database with the functions of forward one record, backward one record, go to the first record and go to the last record. The info of staff in the database includes name, gender, age, staff number, office phone number and office room number.

    Other requirements:
    I must have Base class to encase name, gender and age and a Derived class to encase staff number, office phone number and office room number.

    -The database (text file) looks like this:

    Dr Sean Kingston
    M
    32
    f10000
    51790689
    B123
    Ms Mandy Moore
    F
    28
    f10001
    52721423
    BC2345
    Mr Alexander Graham Bell
    M
    30
    f10003
    53792162
    B234
    Mr Thomas Alfa Edison
    M
    31
    f10004
    59097123
    BD78910

    - The code that I have written:
    It displays the info successfully, but I dunno display the next record, the previous record as the number of characters in each field may differ. Do I have to open the file in binary mode? How do we do that? Sample code is appreciated. Thanks

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class Base
    {
    protected:
    string name;
    char gender;
    int age;
    };
    
    class Derived : public Base
    {
    protected:
    string staffno;
    int telpno;
    string roomno;
    public:
    void enquiry (void);
    };
    
    void Derived::enquiry ()
    {
    int n;
    fstream f;
    f.open("info.txt");
    while (1)
    {
    getline (f, name);
    f >> gender;
    f >> age;
    f >> staffno;
    f >> telpno;
    f >> roomno;
    
    cout << "Name: " << name;
    cout << "\nGender: " << gender;
    cout << "\nAge: " << age;
    cout << "\nStaff No.: " << staffno;
    cout << "\nPhone No.: " << telpno;
    cout << "\nRoom No.: " << roomno << endl <<endl;
    
    cout << "<1> previous \t <2> next \t <0> exit\n";
    cin >> n;
    
    // I dunno what to do from here onwards..
    
    }
    f.close();
    
    }
    
    void main ()
    { 
    Derived staff;
    staff.enquiry();
    }
    
     
  2. cherries

    cherries New Member

    Joined:
    Dec 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    sooolveeed! thanks~~
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice