Thread: Define a function

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Define a function

    I do not know where to start with this requirement. I know I have to int isNull() and write it to a file, but not really know how to const it.

    Just need an example on how to start this, thanks

    Need to define function isNull() in personal class and define function writeNullToFile to overwrite a record to be deleted by a null record. The null record is defined as having a nonnumeric character (a tombstone) in the first position of the SSN member.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    #ifndef PERSONAL
    #define PERSONAL
    
    class Personal 
    {
    public:
        Personal();
        Personal(char*,char*,char*,int,long);
        void writeToFile(fstream&) const;
        void readFromFile(fstream&);
        void readKey();
        int size() const 
    	{
            return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
        }
        bool operator==(const Personal& pr) const 
    	{
            return strncmp(pr.SSN,SSN,9) == 0;
        }
    protected:
        const int nameLen, cityLen;
        char SSN[10], *name, *city;
        int year;
        long salary;
        ostream& writeLegibly(ostream&);
        friend ostream& operator<<(ostream& out, Personal& pr) 
    	{
            return pr.writeLegibly(out);
        }
        istream& readFromConsole(istream&);
        friend istream& operator>>(istream& in, Personal& pr) 
    	{
            return pr.readFromConsole(in);
        }
    };
    
    #endif
    
    
    
    
    
    
    
    #include "personal.h"
    
    Personal::Personal() : nameLen(10), cityLen(10) 
    {
        name = new char[nameLen+1];
        city = new char[cityLen+1];
    }
    
    Personal::Personal(char ........n, char *n, char *c, int y, long s) :
            nameLen(10), cityLen(10) 
    {
        name = new char[nameLen+1];
        city = new char[cityLen+1];
        strcpy(SSN,ssn);
        strcpy(name,n);
        strcpy(city,c);
        year = y;
        salary = s;
    }
    
    void Personal::writeToFile(fstream& out) const 
    {
        out.write(SSN,9);
        out.write(name,nameLen);
        out.write(city,cityLen);
        out.write(reinterpret_cast<const char*>(&year),sizeof(int));
        out.write(reinterpret_cast<const char*>(&salary),sizeof(int));
    }
    
    void Personal::readFromFile(fstream& in) 
    {
        in.read(SSN,9);
        in.read(name,nameLen);
        in.read(city,cityLen);
        in.read(reinterpret_cast<char*>(&year),sizeof(int));
        in.read(reinterpret_cast<char*>(&salary),sizeof(int));
    }
    
    void Personal::readKey() 
    {
        char s[80];
        cout << "Enter SSN: ";
        cin.getline(s,80);
        strncpy(SSN,s,9);
    }
    
    ostream& Personal::writeLegibly(ostream& out) 
    {
        SSN[9] = name[nameLen] = city[cityLen] = '\0';
        out << "SSN = " << SSN << ", name = " << name
            << ", city = " << city << ", year = " << year
            << ", salary = " << salary;
        return out;
    }
    
    istream& Personal::readFromConsole(istream& in) 
    {
        char s[80];
        cout << "SSN: ";
        in.getline(s,80);
        strncpy(SSN,s,9);
        cout << "Name: ";
        in.getline(s,80);
        strncpy(name,s,nameLen);
        cout << "City: ";
        in.getline(s,80);
        strncpy(city,s,cityLen);
        cout << "Birthyear: ";
        in >> year;
        cout << "Salary: ";
        in >> salary;
        in.getline(s,80); // get '\n'
        return in;
    }

  2. #2
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    What exactly are you having trouble with? Defining the method, manipulating the file?

    May I ask if this is homework?
    Last edited by rynoon; 01-04-2007 at 04:57 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What you could do, is set the Personal's data to null-data and use the existing method to write to the file.

    Aside from that, I don't see how Personal could be allowed to do what you are required. Why should Mary Smith's personal record be able to delete John Doe's record???

    Also, to overwrite a record, the class would need to know its position in the file. I don't see how this information fits logically into the Personal class (well, you could pass file position as a parameter). Anyway, managing the data-base should not be the task of database data.

    (Edit) Reread the post, it appears that deleting the record is done by an external function. Well, I can think of two simple approaches: either add a bool to the Personal class telling if it is empty or not, or set Personal data to empty data (as described in the requirements) and check this to decide later if the object is empty.

    By the way, there is also a memory leak: you allocate memory in the constructor (which you don't need to do, since string lengths are constant), but there is no destructor to delete this memory. Also, because of dynamic memory allocation this class needs a copy constructor or it should be made non-copyable (which probably isn't an option).
    Last edited by anon; 01-04-2007 at 09:04 AM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    I am having a problem defining the method within the class.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Which method? This one?
    define function writeNullToFile to overwrite a record to be deleted by a null record
    It doesn't make sense for Personal to overwrite any records in a file.

    If you insist so, the signature might be:
    Code:
    void Personal::writeNullToFile(fstream&, int where, int howManyBytes);
    Write any data to this place, but change the first character of SSN to a non-number before writing it to file.

    The isNull method would simply return, if the first character of SSN is non-numeric.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    The isNull method, how would I start the int.

    int isNull()

    int a

    if (isNull() = a)

    wruteNullTo File

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by CRC05
    The isNull method, how would I start the int.

    int isNull()

    int a

    if (isNull() = a)

    wruteNullTo File
    I don't understand the question, but for comparison you should use ==
    if(isNull() == a)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 10:07 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM