code begins here
--------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
struct Bartender
{
char ID[50];
char name[50];
};
struct Node
{
Bartender node_btd;
Node *next;
};
Node *root = NULL, *current;
ofstream outfile;
ifstream infile;
| |
--------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12
|
void Database::add_btd(){
Bartender btd;
cout << "Enter a new Bartender ID: ";
cin.getline(btd.ID,50);
cout << "Enter the new Bartender name: ";
cin.getline(btd.name,50);
outfile.open("bartender.txt", ios::out|ios::app|ios::binary);
outfile.write((char*)&btd, sizeof(btd));
outfile.close();
}
| |
--------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void Database::get_btd_list(){
Bartender btd;
Node *temp, *temp2;
temp = new Node;
temp2 = new Node;
infile.open("bartender.txt", ios::in|ios::binary);
while(infile.read((char*)&btd, sizeof(btd))){
strcpy(temp->node_btd.ID, btd.ID);
strcpy(temp->node_btd.name, btd.name);
temp->next = NULL;
if(root == NULL){
root = temp;
current = root;
}
else{
temp2 = root;
while(temp2->next != NULL){
temp2 = temp2->next;
}
temp2->next = temp;
}
}
infile.close();
}
| |
--------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void Database::display_btd(){
Node *temp;
temp = root;
if(temp == NULL){
cout << "No bartenders found" << endl;
}
else{
while(temp != NULL){
cout << temp->node_btd.ID << "\t" << temp->node_btd.name << endl;
temp = temp->next;
}
}
}
| |
--------------------------------------------------------------------
code ends here.
what Im trying to do is that get input from user and write it into "bartender.txt". Then I wanna make a linked list from the file.
add_btd() write to file.
get_btd_list() make a linked list from the file.
display_btd() displays the list of bartenders.
It works when there is just one bartender in the file.
After I input some more bartenders, it doesn't stop displaying the last bartender Ive input. not even sure if it dispalys the previous ones.
those structures 'Bartender' and 'Node', and 'root' and 'current' are declared as global in bar.h and the functions are in bar.cpp.
I would appreciate if you could share some of your knowledge..