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

Recommended Answers

All 5 Replies

If you mean why it doesn't run, the answer is because it's a class implementation and you need to have another program to use this class. You don't even have main function, so how do you expect this code to run in the first place????

If you mean why it doesn't run, the answer is because it's a class implementation and you need to have another program to use this class. You don't even have main function, so how do you expect this code to run in the first place????

no, what i mean is when I run the test driver which contain the main function it does not call any of the class function please see below the test driver

#include"hybrid.h"
#include<iostream>

using namespace std;

int main()
{
 HybridList object;
 for(int i = 0; i < 3; i++)
  {
  object.ReStart();

   cout<< " Licensea file has been sorted and written to new file" <<endl;
   }
}

It runs for me and calls the GetNext function three times, as expected. What is your output and does it run to completion?

I don't see why your code doesn't run. Your GetNext() method should be called three times as stated by VernonDozier. Hence, the same piece of information is printed on screen three times. The only reason for the program no running properly (that I can think of) is the license.txt file is missing.

It runs for me and calls the GetNext function three times, as expected. What is your output and does it run to completion?

Thank you for your assistance.

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.