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"
}

Please use code tags [code]//code goes here [/code]

In your GetString() function, add a cin.ignore() after the cin.get. There's probably a stray '\n' in the stream which gets picked up by the cin call in the GetNumber() function.

Edited 3 Years Ago by Dani: Formatting fixed

**Please use code tags in the future**

First, one little thing that often comes up, your GetNumber function should include a "cin.ignore();" call to clean the input buffer after entering a numeric value of any kind. So it should be:

void GetNumber( )
{
  cin >> reco.Dlicense;
  cin.ignore();
}

Now, the biggest problem is your SaveDB function. It is so wrong that I can't really make much sense of it. But I know it is wrong for several reasons:

void Tbuiltdoc::SaveDB(Record *rec)
{
  NodePtr newNodePtr = rec; //you set newNodePtr to rec here, but then..

  newNodePtr = new Record; // you give it another value which is a pointer to a new record.
  currentPtr = newNodePtr; // then you sen currentPtr to that same new record.

  ofstream ofs(fn, ios::binary | ios::app);
  if(!ofs) 
    cout<<"error";
  ofs.write((const char*) rec, sizeof(Record)); //this is very wrong, you need to save the individual components because rec contains pointers.. the addresses will be saved in the file and will be completely meaningless when you load it again.
  ofs.close();
  if(first != 0) //What is the value of first.. should it not be "head"
    head = currentPtr; //if first record
  currentPtr = currentPtr->Link; //at this point, currentPtr points to an uninitialized record, and Link is a garbage value.
  delete newNodePtr; //now you delete newNodePtr, which is OK because newNodePtr has always been meaningless to start with, but normally it should be a new link in the list and should not be deleted but kept in the list.
  first--; //hein?
}

So, the conclusion is that this function has only negative effects and accomplishes nothing. On the first call, it will leave both "head" and "currentPtr" to completely corrupt values, it will not save the new record in the list, and it will save corrupt data to a binary file. I don't mean to be mean, but that's just the fact and it is hard for me to see what you expected your code to actually do.

This question has already been answered. Start a new discussion instead.