?
Solved

Need help separating user display code in main into a Class

Posted on 2004-11-29
11
Medium Priority
?
240 Views
Last Modified: 2011-09-20
I have a program which manipulates data records through a file stream.  The program works fine when all of the file manipulation and user display functions are defined in the main program.  I need to separate all of these functions into a Class.  
First question-  What should the constructor and destructor look like when there are no data members in the class?
Second question- I have tried several different ways to create the new Class, but I keep getting compiler errors (usually C2440) which point to this line of code:
                   void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord, deleteRecord};

Here is the code before creating the new Class:  (if you want to compile this, create a blank file called names.dat for the data)
//main.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;
#include <cstring>
#include <cctype>
#include <cstdlib>
#include "EMailObjs.h"

// function prototypes for managing the random file
void initializeFile( fstream & );
void inputData( fstream & );
void listEMailObjs( fstream & );
void updateRecord( fstream & );
void insertRecord( fstream & );
void deleteRecord( fstream & );
int instructions( void );
bool read_integer( istream&, int& );

int main()
{
   int choice;
   char response;

   // file stream used for input and output
   fstream file( "C:\\names.dat", ios::in | ios::out );
   void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord,
      deleteRecord};
   
   // terminate program if file cannot be opened
   if ( !file ) {
      cerr << "File could not be opened.\n";
      exit( EXIT_FAILURE );
   
   } // end if

   // ask user if new file should be made
   cout << "Should the file be initialized (Y or N): ";
   cin >> response;  

   // test if users response was valid
   while ( toupper( response ) != 'Y' && toupper( response ) != 'N' ) {
      cout << "Invalid response. Enter Y or N: ";
      cin >> response;
   
   } // end while

   // initialize file if user says too
   if ( toupper( response ) == 'Y' ) {
      initializeFile( file );
      inputData( file );
   
   } // end if

   // perform user instructions until 5 is entered
   while ( ( choice = instructions() ) != 5 ) {
      ( *f[ choice - 1 ] )( file );
      file.clear();    // reset eof indicator
   
   } // end while

   file.close(); // close input/output file

   return 0;

} // end main

bool read_integer ( istream& is, int& i)
{
    int n;
    string str;
    char* pcCnvEnd = NULL;

    getline(is,str); // read input

    n = (int) strtol(str.c_str(),&pcCnvEnd,10); // try to convert the string to int

    if ( *pcCnvEnd) // conversion did not end at the term. NULL -> invalid input
      return false;

    i = n; // conversion successful, was an int

    return true;
}

// function to clear file
void initializeFile( fstream &fRef )
{
   EMailObjs blankItem;  // empty EMailObjs object

   // fill file with blank records
   for ( int i = 0; i < 100; ++i )
      fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( EMailObjs ) );

} // end function initializeFile

// function that recieves input
void inputData( fstream &fRef )
{
   EMailObjs temp;  // temporary EMailObjs object

   // temporary variables used to hold user input
   int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];

   // ask user for and set partNumber
   cout << "Enter the record number (0 - 99, -1 to end input): ";
   cin >> number;
   temp.setRecordNumber( number );

   // set EMailObjs members until Q is entered
   while ( temp.getRecordNumber() != -1 ) {
        cout << "Enter the name: "; // ask user for name
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );   // store namel in temporary variable name
        temp.setName( Aname );  // set name
      cout << "Enter the email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the website: ";  // ask user for website
        cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website
       
      // place file-position pointer at next write location
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );

      // write data to file
      fRef.write( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );

      // ask user for next part number
      cout << "Enter the record number (0 - 99, -1 to end input): ";
      cin >> number;
      temp.setRecordNumber( number );

   } // end while

} // end inputData

// function that decides what choice user selected
int instructions( void )
{
   int choice;

   // ask user to enter a choice
   cout << "\nEnter a choice:\n" <<
                  "1  List all records.\n" <<
                  "2  Change record.\n" <<
                  "3  Add a record.\n" <<
                  "4  Delete a record.\n" <<
                  "5  Quit.\n";

   // ask user for choice until a valid choice is entered
   do {
      if ( !read_integer(cin,choice)) continue; // go on in the loop if input was invalid
        }
   while ( choice < 1 || choice > 5 );

   return choice; // return user choice

} // end function instructions

// function that lists tools in file
void listEMailObjs( fstream &fRef )
{
   EMailObjs temp;

   // display column headings
   cout << '\n' << setw( 3 )  << " " << left
        << setw( 12 ) << "Name" << left
            << setw( 20 ) << "EMail" << left
            << setw( 20 ) << "Website" << left
        << '\n';

   // continue until 100 EMailObjss are displayed or end of file reached
   for ( int count = 0; count < 100 && !fRef.eof(); ++count ) {
     
      // set file position pointer and begin reading
      fRef.seekg( count * sizeof( EMailObjs ) );      
      fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );

      // if part number is valid, display EMailObjs information
      if ( temp.getRecordNumber() >= 0 && temp.getRecordNumber() < 100 ) {
              cout << setw( 2 ) << temp.getRecordNumber() << " " 
              << left << setw( 12 ) << temp.getName().data()
              << left << setw( 20 ) << temp.getEmailAddress().data()
                    << left << setw( 20 ) << temp.getWebsite().data()
              << '\n';
     
      } // end if

   } // end for
   
} // end function listEMailObjss

// function to update a EMailObjs's information
void updateRecord( fstream &fRef )
{
   EMailObjs temp;
   int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];

   // ask user what part to update
   cout << "Enter the record number for update: ";
   cin >> number;

   // set file positions pointer to correct EMailObjs
   fRef.seekg( number * sizeof( EMailObjs ) );

   // read EMailObjs information
   fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );

   // display EMailObjs information if partNumber is not -1
   if ( temp.getRecordNumber() != -1 ) {
      cout << '\n' << setw( 3 ) << " " << left
        << setw( 12 ) << "Name" << left
            << setw( 20 ) << "EMail" << left
            << setw( 20 ) << "Website" << left
        << '\n';

         cout << setw( 2 ) << temp.getRecordNumber() << " " 
              << left << setw( 12 ) << temp.getName().data()
              << left << setw( 20 ) << temp.getEmailAddress().data()
                    << left << setw( 20 ) << temp.getWebsite().data()
              << '\n';
        cout << "Enter the new name: "; // ask user for new name

      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );   // set new name
      temp.setName( Aname );  
      cout << "Enter the new email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the new website: ";  // ask user for website
        cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website

      // set file position pointer and write information to file
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * > ( &temp ), sizeof( EMailObjs ) );
   
   } // end if
   else
      cerr << "Cannot update. The record is empty.\n";

} // end function updateRecord

// function to insert a new record
void insertRecord( fstream &fRef )
{
      EMailObjs temp;
      int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];

   // ask user for record number
   cout << "Enter the record number for insertion: ";
   cin >> number;

   // set file position pointer and read data from file
   fRef.seekg( ( number ) * sizeof( EMailObjs ) );
   fRef.read( reinterpret_cast< char * > ( &temp ), sizeof( EMailObjs ) );

   // as long as record is empty get information from user
   if ( temp.getRecordNumber() == -1 ) {
      temp.setRecordNumber( number );   // set recordNumber
      cout << "Enter the name: "; // ask user for name
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );
      temp.setName( Aname );  // set Name
      cout << "Enter the email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the website: ";  // ask user for website
        cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website

      // set file position pointer and write information to file
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );
   
   } // end if
   else
      cerr << "Cannot insert. The record contains information.\n";

} // end function insertRecord

// function to delete a record
void deleteRecord( fstream &fRef )
{
   EMailObjs blankItem;
   EMailObjs temp;
   int number;

   // get tool user wants to delete
   cout << "Enter the record number for deletion: ";
   cin >> number;

   // set file position pointer and read information from file
   fRef.seekg( number * sizeof( EMailObjs ) );
   fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );

   // if record contains data, set record to an empty EMailObjs object
   if ( temp.getRecordNumber() != -1 ) {
      fRef.seekp( number * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( EMailObjs ) );
      cout << "Record deleted.\n";
   
   } // end if
   else
      cerr << "Cannot delete. The record is empty.\n";
} // end function deleteRecord

////////////////////////////////////////////////////////////////////////////////
//EMailObjs.h
#ifndef EMAILOBJS_H
#define EMAILOBJS_H

#include <iostream>

using namespace std;

const int LENGTH = 50;

class EMailObjs
{
public:
      EMailObjs(int= -1, string = "", string = "", string = "");  //default constructor
      void setRecordNumber( int );
    int getRecordNumber() const;
      void setName(string);
      string getName()const;
      void setEmailAddress(string);
      string getEmailAddress()const;
      void setWebsite(string);
      string getWebsite()const;
private:
      int recordNumber;
      char name[LENGTH];                  // name of person or company
      char email[LENGTH];      // email address
      char website[LENGTH];                  // website address
};

#endif
//////////////////////////////////////////////////////////////////////////////////////////////
//EMailObjs.cpp
#include <iostream>

using namespace std;

#include <cstring>
#include "EMailObjs.h"

EMailObjs::EMailObjs(int recordNumberValue, string nameValue, string emailValue, string websiteValue)
{
      setRecordNumber( recordNumberValue );
      setName(nameValue);
      setEmailAddress(emailValue);
      setWebsite(websiteValue);
}

void EMailObjs::setRecordNumber( int recordNumberValue )
{
   recordNumber = recordNumberValue;
}

int EMailObjs::getRecordNumber() const
{
   return recordNumber;
}

void EMailObjs::setName(string nameString)
{
      const char *nameValue = nameString.data();
      int length = strlen(nameValue);
      length = (length < 50 ? length : 49);
      strncpy(name, nameValue, length);
      name[length] = '\0';
}

string EMailObjs::getName()const
{
      return name;
}

void EMailObjs::setEmailAddress(string emailString)
{
      const char *emailValue = emailString.data();
      int length = strlen(emailValue);
      length = (length < 50 ? length : 49);
      strncpy(email, emailValue, length);
      email[length] = '\0';
}

string EMailObjs::getEmailAddress()const
{
      return email;
}

void EMailObjs::setWebsite(string websiteString)
{
      const char *websiteValue = websiteString.data();
      int length = strlen(websiteValue);
      length = (length < 50 ? length : 49);
      strncpy(website, websiteValue, length);
      website[length] = '\0';
}

string EMailObjs::getWebsite()const
{
      return website;
}

0
Comment
Question by:coririzzo
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12700151
What is the exact error message?
0
 

Author Comment

by:coririzzo
ID: 12700845
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(std::fstream &)'
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12700867
> First question-  What should the constructor and destructor look like when there are no data members in the class?

Don't bother writing them.

> Second question- I have tried several different ways to create the new Class, but I keep getting compiler errors (usually C2440) which point to this line of code:
                   void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord, deleteRecord};

It compiles with no errors for me on VC 6 and 7. I have to say that it doesn't look very much in the spirit of C++ to me, though.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Axter
ID: 12700963
>>> Second question- I have tried several different ways to create the new Class, but I keep getting compiler errors (usually C2440) which point to this line
>>of code:
>>                  void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord, deleteRecord};

It also compiles on my compiler (VC++ 6.0).

Are you sure you're posting the exact code?
0
 
LVL 30

Expert Comment

by:Axter
ID: 12700973
>>error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(std::fstream &)'

Exactly what line is the above error for?
0
 

Author Comment

by:coririzzo
ID: 12701111
The error is for this line:

 void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord, deleteRecord};

I am running Visual C++.NET.

I have actually deleted this line and added a switch structure to the while loop and it works fine now.
0
 

Author Comment

by:coririzzo
ID: 12702150
I just realized that you all were thinking that the compiler errors were from the code that I posted.  That actually compiles just fine.  It is when I put the data manipulation functions into their own Class that I have problems.  Here is that code:
//main.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
#include "EMailObjs.h"
#include "UserInterface.h"
using namespace std;

int main()
{
   UserInterface test;
   test.display();
   return 0;
} // end main
////////////////////////////////////////////////////////////////
//EMailObjs.h
#ifndef EMAILOBJS_H
#define EMAILOBJS_H

#include <iostream>
using std::string;

const int LENGTH = 50;

class EMailObjs{
public:
      EMailObjs(int= -1, string = "", string = "", string = "");  //default constructor
      void setRecordNumber( int );
                int getRecordNumber() const;
      void setName(string);
      string getName()const;
      void setEmailAddress(string);
      string getEmailAddress()const;
      void setWebsite(string);
      string getWebsite()const;
private:
      int recordNumber;
      char name[LENGTH];                  // name of person or company
      char email[LENGTH];      // email address
      char website[LENGTH];                  // website address
};
#endif
////////////////////////////////////////////////////////////////////////////////////
//EMailObjs.cpp
#include <iostream>
using std::string;

#include <cstring>
#include "EMailObjs.h"

EMailObjs::EMailObjs(int recordNumberValue, string nameValue, string emailValue, string websiteValue){
      setRecordNumber( recordNumberValue );
      setName(nameValue);
      setEmailAddress(emailValue);
      setWebsite(websiteValue);
}

void EMailObjs::setRecordNumber( int recordNumberValue ){
   recordNumber = recordNumberValue;
}

int EMailObjs::getRecordNumber() const{
   return recordNumber;
}

void EMailObjs::setName(string nameString){
      const char *nameValue = nameString.data();
      int length = strlen(nameValue);
      length = (length < 50 ? length : 49);
      strncpy(name, nameValue, length);
      name[length] = '\0';
}

string EMailObjs::getName()const{
      return name;
}

void EMailObjs::setEmailAddress(string emailString){
      const char *emailValue = emailString.data();
      int length = strlen(emailValue);
      length = (length < 50 ? length : 49);
      strncpy(email, emailValue, length);
      email[length] = '\0';
}

string EMailObjs::getEmailAddress()const{
      return email;
}

void EMailObjs::setWebsite(string websiteString){
      const char *websiteValue = websiteString.data();
      int length = strlen(websiteValue);
      length = (length < 50 ? length : 49);
      strncpy(website, websiteValue, length);
      website[length] = '\0';
}

string EMailObjs::getWebsite()const{
      return website;
}
///////////////////////////////////////////////////////////////////////////////////////
//UserInterface.h
#ifndef USERINTERFACE_H
#define USERINTERFACE_H

#include <iostream>
using namespace std;

class UserInterface{
public:
void initializeFile( fstream & );
int display();
// function prototypes for managing the random file
void inputData( fstream & );
void listEMailObjs( fstream & );
void updateRecord( fstream & );
void insertRecord( fstream & );
void deleteRecord( fstream & );
int instructions( );
};
#endif
///////////////////////////////////////////////////////////////////////////////////////
//UserInterface.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
#include "UserInterface.h"
#include "EMailObjs.h"
using namespace std;

// function to clear file
void UserInterface::initializeFile( fstream &fRef ){
   EMailObjs blankItem;  // empty EMailObjs object
   // fill file with blank records
   for ( int i = 0; i < 100; ++i )
      fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( EMailObjs ) );
} // end function initializeFile

// function that recieves input
void UserInterface::inputData( fstream &fRef ){
   EMailObjs temp;  // temporary EMailObjs object
   // temporary variables used to hold user input
                int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];
   // ask user for and set partNumber
   cout << "Enter the record number (0 - 99, -1 to end input): ";
   cin >> number;
   temp.setRecordNumber( number );
   // set EMailObjs members until Q is entered
   while ( temp.getRecordNumber() != -1 ) {
     cout << "Enter the name: "; // ask user for name
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );   // store namel in temporary variable name
      temp.setName( Aname );  // set name
      cout << "Enter the email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the website: ";  // ask user for website
     cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website
       // place file-position pointer at next write location
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );
      // write data to file
      fRef.write( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );
      // ask user for next part number
    cout << "Enter the record number (0 - 99, -1 to end input): ";
      cin >> number;
      temp.setRecordNumber( number );
   } // end while
} // end inputData

// function that decides what choice user selected
int instructions(){
   int choice;
   // ask user to enter a choice
   cout << "\nEnter a choice:\n" <<
                  "1  List all records.\n" <<
                  "2  Change record.\n" <<
                  "3  Add a record.\n" <<
                  "4  Delete a record.\n" <<
                  "5  Quit.\n";
   // ask user for choice until a valid choice is entered
   do {
      cout << "? ";
      cin >> choice;
   } while ( choice < 1 || choice > 5 );
   return choice; // return user choice
} // end function instructions

// function that lists tools in file
void UserInterface::listEMailObjs( fstream &fRef ){
   EMailObjs temp;
   // display column headings
   cout << '\n' << setw( 3 )  << " " << left
        << setw( 12 ) << "Name" << left
            << setw( 20 ) << "EMail" << left
            << setw( 20 ) << "Website" << left
        << '\n';
   // continue until 100 EMailObjss are displayed or end of file reached
   for ( int count = 0; count < 100 && !fRef.eof(); ++count ) {
       // set file position pointer and begin reading
      fRef.seekg( count * sizeof( EMailObjs ) );      
      fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );
      // if part number is valid, display EMailObjs information
      if ( temp.getRecordNumber() >= 0 && temp.getRecordNumber() < 100 ) {
              cout << setw( 2 ) << temp.getRecordNumber() << " " 
              << left << setw( 12 ) << temp.getName().data()
              << left << setw( 20 ) << temp.getEmailAddress().data()
      << left << setw( 20 ) << temp.getWebsite().data() << '\n';
       } // end if
   } // end for
} // end function listEMailObjss

// function to update a EMailObjs's information
void UserInterface::updateRecord( fstream &fRef ){
   EMailObjs temp;
   int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];
   // ask user what part to update
   cout << "Enter the record number for update: ";
   cin >> number;
   // set file positions pointer to correct EMailObjs
   fRef.seekg( number * sizeof( EMailObjs ) );
   // read EMailObjs information
   fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );
   // display EMailObjs information if partNumber is not -1
   if ( temp.getRecordNumber() != -1 ) {
      cout << '\n' << setw( 3 ) << " " << left
        << setw( 12 ) << "Name" << left
     << setw( 20 ) << "EMail" << left
     << setw( 20 ) << "Website" << left << '\n';

         cout << setw( 2 ) << temp.getRecordNumber() << " " 
              << left << setw( 12 ) << temp.getName().data()
              << left << setw( 20 ) << temp.getEmailAddress().data()
      << left << setw( 20 ) << temp.getWebsite().data()<< '\n';
        cout << "Enter the new name: "; // ask user for new name

      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );   // set new name
      temp.setName( Aname );  
      cout << "Enter the new email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the new website: ";  // ask user for website
        cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website

      // set file position pointer and write information to file
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * > ( &temp ), sizeof( EMailObjs ) );
  } // end if
   else
      cerr << "Cannot update. The record is empty.\n";
} // end function updateRecord

// function to insert a new record
void UserInterface::insertRecord( fstream &fRef ){
      EMailObjs temp;
      int number;
      char Aname[LENGTH];                  
      char Aemail[LENGTH];      
      char Awebsite[LENGTH];
   // ask user for record number
   cout << "Enter the record number for insertion: ";
   cin >> number;
   // set file position pointer and read data from file
   fRef.seekg( ( number ) * sizeof( EMailObjs ) );
   fRef.read( reinterpret_cast< char * > ( &temp ), sizeof( EMailObjs ) );

   // as long as record is empty get information from user
   if ( temp.getRecordNumber() == -1 ) {
      temp.setRecordNumber( number );   // set recordNumber
      cout << "Enter the name: "; // ask user for name
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aname, LENGTH );
      temp.setName( Aname );  // set Name
      cout << "Enter the email address: "; // ask user for email address
      cin.ignore();  // ignore the newline on the input stream
      cin.get( Aemail, LENGTH );   // store EMail in temporary variable
      temp.setEmailAddress( Aemail );  // set EMail address
      cout << "Enter the website: ";  // ask user for website
        cin.ignore();  // ignore the newline on the input stream
      cin.get( Awebsite, LENGTH );  // store input in temporary variables
      temp.setWebsite( Awebsite );  // set website

      // set file position pointer and write information to file
      fRef.seekp( ( temp.getRecordNumber() ) * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );
   
   } // end if
   else
      cerr << "Cannot insert. The record contains information.\n";
} // end function insertRecord

// function to delete a record
void UserInterface::deleteRecord( fstream &fRef ){
   EMailObjs blankItem;
   EMailObjs temp;
   int number;

   // get tool user wants to delete
   cout << "Enter the record number for deletion: ";
   cin >> number;

   // set file position pointer and read information from file
   fRef.seekg( number * sizeof( EMailObjs ) );
   fRef.read( reinterpret_cast< char * >( &temp ), sizeof( EMailObjs ) );

   // if record contains data, set record to an empty EMailObjs object
   if ( temp.getRecordNumber() != -1 ) {
      fRef.seekp( number * sizeof( EMailObjs ) );
      fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( EMailObjs ) );
      cout << "Record deleted.\n";
    } // end if
   else
      cerr << "Cannot delete. The record is empty.\n";
} // end function deleteRecord

int UserInterface::display(){
      int choice;
      char response;
   // file stream used for input and output
   fstream file( "C:\\names.dat", ios::in | ios::out );
   void ( *f[] )( fstream & ) = { listEMailObjs, updateRecord, insertRecord,
      deleteRecord };
    // terminate program if file cannot be opened
   if ( !file ) {
      cerr << "File could not be opened.\n";
      exit( EXIT_FAILURE );
   } // end if
   // ask user if new file should be made
   cout << "Should the file be initialized (Y or N): ";
   cin >> response;  
   // test if users response was valid
   while ( toupper( response ) != 'Y' && toupper( response ) != 'N' ) {
      cout << "Invalid response. Enter Y or N: ";
      cin >> response;
   } // end while
   // initialize file if user says too
   if ( toupper( response ) == 'Y' ) {
      initializeFile( file );
      inputData( file );
   } // end if
   // perform user instructions until 5 is entered
  while ( ( choice = instructions() ) != 5 ) {
      ( *f[ choice - 1 ] )( file );
      file.clear();    // reset eof indicator
   } // end while
    file.close(); // close input/output file
   return 0;
}




0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12704193
Yuor problem is that UserInterface::listEMailObjs( fstream & ) etc are class function. Make them static and you should be in business.
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 2000 total points
ID: 12704215
i.e.
--------8<--------
class UserInterface{
public:
static void initializeFile( fstream & );
int display();
// function prototypes for managing the random file
static void inputData( fstream & );
static void listEMailObjs( fstream & );
static void updateRecord( fstream & );
static void insertRecord( fstream & );
static void deleteRecord( fstream & );
int instructions( );
};
--------8<--------

NB: You also need to fix:

--------8<--------
// function that decides what choice user selected
int UserInterface::instructions(){
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12704228
Alternatively, since UserInterface has no class data and all you are really doing is typing together a bunch of non-OO functions with a class name, you could declare UserInterface as a namespace.

i.e.
--------8<--------
namespace UserInterface {
void initializeFile( fstream & );
int display();
// function prototypes for managing the random file
 void inputData( fstream & );
void listEMailObjs( fstream & );
void updateRecord( fstream & );
void insertRecord( fstream & );
void deleteRecord( fstream & );
int instructions( );
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12704237
What this really coes back to, though, is that you are not using an object orientated design. Arrays of function pointers are C-ish things to work with.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question