Hi I am a newbie in C++. I need some help here for my homework. I tried all methods that I know but I just can't get it. =(

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

#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();
}

Recommended Answers

All 7 Replies

but I dunno display the next record, the previous record as the number of characters in each field may differ.

Doesn't matter if they differ. It might matter in case you want to write the new records at the place where the current record is displayed. In that case, clear that part of the screen before displaying the next/previous record..

I don't need to write any new records. What I don't know is how to display the previous record since the position of the buffer has moved.

For example,
1st record has 44 chars
2nd record has 47 chars
3rd record has 42 chars

After taking in data from the 1st record, the buffer is at position 45, so we can read the 2nd record directly.

But after taking in dadta from the 2nd record, the buffer is now at position (44+47+1) = 92. When this happens, how do we move the buffer back to 45, knowing that the size of each record differs?

Two solutions:

1)You'll have to insert some special character (* or # or $) between the records which would serve the purpose of a bookmark.

2)Store the size of each record in an integer array and do manipulations accordingly at runtime.

The program would not be flexible. I mean, it can only be display the four records in the text file. if we add another staff in the text file, the program would not be able to run it.

That is only if you want the users to add records manually in the text file. Instead, if you give an option to add record in your program itself, then when you add another staff record to the file, you can internally insert that bookmark, or add the size at the end of the array...

if you give an option to add record in your program itself

This is a better option. By this, you can even encrypt your text file for security purposes. The records remain secure from unwanted tampering as user is unable to view the decrypted version of the record file. Any modification in the records has to be done from within the program itself..

solved! thanks.

Okay. Then click on the link 'Mark as solved' that is below the last post in this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.