can anybody tell what's wrong with this code!
//===========================================================================
// hybrid.cpp --this file implement the hybridlist class
// Time-stamp: <2010-10-29 18:52:46 Dnambembe>
// To compile:
// g++ hybrid.cpp
// Copyright (c) 2010 by Domingos Nambembe. All rights reserved.
//===========================================================================
#include<iostream>
#include<cstddef> //need NULL
#include"hybrid.h"
using namespace std;
typedef NodeType* NodePtr; //node pointer for hybrid class
typedef Record* PTR; // node pointer for data struct
struct Record
{
char NameAddress[128];
long int LicenseN;
PTR Link1;
}Mydata;
struct NodeType
{
ComponentType component;
NodePtr Link;
};
Record Data; //global data
// method that construct an empty list
HybridList::HybridList()
// constructor
// postcondition: head == NULL
{
head == NULL;
}
// a method that test for empty list
bool HybridList::IsEmpty() const
{
return (head == NULL); // return true if head == NULL.
}
// inserting item function
void HybridList::Insert( )
{
// precondition: component members of the list are in ascending order
//postcondition: new Node containing item is in proper place
NodePtr newNodePtr = new NodeType; // set-up node to be inserted
newNodePtr->component = Data;
NodePtr prevPtr = NULL;
NodePtr currentPtr = head;
while(currentPtr != NULL && Data.LicenseN > currentPtr->component.LicenseN)
{
prevPtr = currentPtr;
currentPtr = currentPtr->Link; // searching for proper place for item
}
if(prevPtr->component.LicenseN == newNodePtr->component.LicenseN)return;
// if license number are iqual exit
// insert item
ComponentType newData = newNodePtr->component; //pass the struct record
SaveDB(&newData); //write the sorted data to new file
newNodePtr->Link = currentPtr;
if (prevPtr == NULL) head = newNodePtr;
else
prevPtr->Link = newNodePtr;
delete newNodePtr;
}
// class destructor for
HybridList::~HybridList()
{
ComponentType temp; // temporary variable
// while(!IsEmpty()) RemoveFirst(temp);
}
// implementation for GetNext() function
bool HybridList::GetNext()
{
ifstream bofs(OUTDATA, ios::in | ios::binary | ios::ate);
//bofs.seekg(0, ios::beg);
if(!bofs) cout<< "Unable to open file"<<endl;
bofs.read((char*)&Mydata, sizeof(Data));
Data = Mydata;
cout<< Mydata.NameAddress<<endl;
cout<<Mydata.LicenseN<<endl;
bofs.close();
}
// Restart
void HybridList::ReStart()
{
GetNext();
}
// writing new file
void HybridList::SaveDB(Record *rec)
{
ofstream ofs(fn, ios::binary | ios::app);
if(!ofs) cout<<"error";
ofs.write((const char*) rec, sizeof(Record));
ofs.close();
}
Or this:
//===========================================================================
// hybrid.h -- Specification file for hybrid class
// Time-stamp: <2010-10-25 19:24:46 Dnambembe>
// To compile:
//
// Copyright (c) 2010 by Domingos Nambembe. All rights reserved.
//===========================================================================
#include<fstream>
#define OUTDATA "licence.txt" // reading from file
#define fn "Noduplicate.text" //sorted file
//the type of each component simple or string
struct NodeType; //Forward declaretion, complete declaretion is hiden
struct Record;
typedef Record ComponentType; // in implementation file
class HybridList
{
public:
bool GetNext(); // extracts record from a file Licensea
void ReStart(); // calls getnext to get data
void SaveDB(Record* K); //create file with no duplicate
bool IsEmpty() const;
//condition:
// function value == true. if list is empty
// == false, otherwise
void Print() const;
// all component in list are output
void InsertAsFirst( ComponentType item);
// precondition
// item < first compnent in the list
void Insert();
// precondition item is not in the list
//postcondition: item inserted && list is in ascending order
void RemoveFirst(/*out*/ ComponentType& item);
// precondition: Not IsEmpty()
// postcondition: item == first component in the list &&
// list in ascending order
void Delete( ComponentType item);
// precondition: item is somewhere in list
//postcondition: item deleted && list in ascending order
HybridList();
// default constractor
// empty list is created
//HybridList(const SortedList& otherlist);
// copy-constractor, list is created as duplicate
~HybridList();
// destructor
// list is destroyed
private:
NodeType* head;
//Record Data;
};