Hi,
I am writing a code that reads input from user to create a linked list database at the same, time it write the data received in to binary file. when I run the program is supposed to prompt the user to enter the data for ten time, however the first time it goes through the for loop it does prompt the user for data and waits until I enter it, but the second and subsequent times the program fails to make a call to cin.getline(), cin and SaveDB(). please help! bellow is the code.
#include "builtdoc.h"
typedef Record* NodePtr;
struct Record
{
char Rcomponent[128];
long int Dlicense;
NodePtr Link;
};
NodePtr head; // pointer to the record struct
NodePtr currentPtr; // anothetr pointer to the record struct
//getting user input
void GetString();
void GetNumber();
// big paste
Record reco ;
int buf_size = 128;
int main()
{
Tbuiltdoc object;
//prompt user to create database
for( int i = 0; i < 10; i++)
{
int buf_size = 128;
cout<< "Enter name and address" <<endl;
GetString();
cout<<"Enter Driver's License Number " <<endl;
GetNumber();
object.SaveDB(&reco); cout<<"Data saved"<<endl;
//else
//cout<< "Unable to save database"<<endl;
}
return 0;
}
void GetString( )
{
cin.get(reco.Rcomponent, buf_size);
}
void GetNumber( )
{
cin >> reco.Dlicense;
}
// implementing the constructor
Tbuiltdoc::Tbuiltdoc()
{
head == NULL;
}
// Destructor
Tbuiltdoc::~Tbuiltdoc()
{
cout<< "Inside the distructor for "<< /*NameAddress*/1 << endl;
cout<< " Press enter to quit" <<endl;
cin.get();
// delete[] NameAddress; // Delete dynamic memory
}
int first = 1;
// to insert first recor
void Tbuiltdoc::SaveDB(Record *rec)
{
NodePtr newNodePtr = rec;
newNodePtr = new Record; // set-up node to be inserted
currentPtr = newNodePtr;
//rec = currentPtr;
ofstream ofs(fn, ios::binary | ios::app);
if(!ofs) cout<<"error";
ofs.write((const char*) rec, sizeof(Record));
ofs.close();
if(first != 0 /*&&IsEmpty()*/) head = currentPtr; //if first record
currentPtr = currentPtr->Link;
delete newNodePtr;
first--;
}
// isempty function
bool Tbuiltdoc::IsEmpty()
{
return(head == NULL); // if no record is created, call "InsertFirst"
}