-2

My cpp file, for some reason I can print everything else beside the name of the book i think its my main file. I don't get any errors when I build it but when I run it the little window comes ups and goes away

#include "BookRecord.h"

BookRecord::BookRecord()
{
    m_sName[128]=NULL;
    m_lStockNum=0;
    m_iClassification = 0;
    m_dCost = 0.0;
    m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
    m_iCount=1;
    m_sName[128]=*name;
    m_lStockNum=sn;
    m_iClassification=cl;
    m_dCost=cost;
}
BookRecord::~BookRecord()
{
}

void BookRecord::getName(char *name)
{
    strcpy(name, m_sName);
}

void BookRecord::setName(char *name)
{
    strcpy(m_sName, name);
}
long BookRecord::getStockNum()
 {
     return m_lStockNum;
 }
void BookRecord::setStockNum(long sn)
{
    m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
    cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
    m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
    *c = m_dCost;
}
void BookRecord::setCost(double c)
    { 
        m_dCost = c;
    }
int BookRecord::getNumberInStock()
 {
     return m_iCount;
 }
 void BookRecord::setNumberInStock(int count)
 {
     count = m_iCount;
 }
void BookRecord::printRecord()
 {
     //cout << "Name: " <<m_sName <<"\n";
     printf("Name: %s", m_sName);
     cout<<endl;
     cout<<"Stock Number: "<<m_lStockNum<<"\n";
     cout<<"Class: " <<m_iClassification<<"\n";
     cout<<"Cost: $"<<m_dCost<<"\n";
     cout<<"In Stock: "<<m_iCount<<"\n";
 }

My main file

#include "BookRecord.h"

int main()
{
    char *bName="C Plus Plus";
    BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
    /*char * bookName="";
    int clNo;
    double c;
    Ana->getClassification(clNo);
    Ana->getCost(&c);
    Ana->getName(bookName);*/
    cout << "Printing using printRecord() Function" << endl;
    Ana->printRecord();

    /*cout << endl << "Printing using getter properties" <<endl;
    cout << bookName << endl;
    cout<< clNo << endl;
    cout << c << endl;
    cout << Ana->getNumberInStock() << endl;*/
    return 0;
}
Vatsal Patel
  • 23
  • 2
  • 6

1 Answers1

2

Hi I would have done this a bit different, not using char m_sName[128], I would rather use std::string or a pointer and just destroyed it in the desctructor. I base my code here on what you have already requested above, and I'm making it as simple as possible. I know things could be done better here, but here you go:

EDIT: Refactored a bit and question grew.

BookRecord.h

#ifndef __BOOKRECORD_H__
#define __BOOKRECORD_H__

#ifndef MAX_NAME_SIZE
#define MAX_NAME_SIZE 128
#endif

class BookRecord
{
private:
    char m_sName[MAX_NAME_SIZE] = {}; // I would have used a pointer or std:string and not char
    long m_lStockNum = 0;
    int m_iClassification = 0;
    double m_dCost = 0.0;
    int m_iCount = 0;

public:
    BookRecord();
    BookRecord(char* name, long sn, int cl, double cost);
    ~BookRecord();
    // EDIT: Your question grew
    char* GetName();
    void SetName(char *name);
    long GetStockNum();
    void SetStockNum(long sn);
    int GetClassification();
    void SetClassification(int cl);
    double GetCost();
    void SetCost(double c);
    int GetNumberInStock();
    void SetNumberInStock(int count);
    void PrintRecord();
};
#endif

BookRecord.cpp

#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "BookRecord.h"
using namespace std;

BookRecord::BookRecord()
{
}

BookRecord::BookRecord(char* name, long sn, int cl, double cost) : 
    m_iCount(1), m_lStockNum(sn), m_iClassification(cl), m_dCost(cost)
{
    if (name == NULL)
        throw std::invalid_argument("Name of book is null");
    if (strlen(name)>MAX_NAME_SIZE)
        throw std::invalid_argument("Name of book is to long");

    memset(&m_sName, 0, sizeof(char)*MAX_NAME_SIZE);
    memcpy(&m_sName, name, strlen(name)*sizeof(char));
}

BookRecord::~BookRecord()
{
}

// Edit: Question grew
char* BookRecord::GetName()
{
    return static_cast<char*>(m_sName);
}

void BookRecord::SetName(char *name)
{
    strcpy(m_sName, name); // TODO: handle null, and sizechecks here to your likings
}

long BookRecord::GetStockNum()
{
    return m_lStockNum;
}

void BookRecord::SetStockNum(long sn)
{
    m_lStockNum = sn;
}

int BookRecord::GetClassification()
{
    return m_iClassification;
}

void BookRecord::SetClassification(int cl)
{
    m_iClassification = cl;
}

double BookRecord::GetCost()
{
    return m_dCost;
}

void BookRecord::SetCost(double c)
{
    m_dCost = c;
}

int BookRecord::GetNumberInStock()
{
    return m_iCount;
}

void BookRecord::SetNumberInStock(int count)
{
    m_iCount = count;
}

void BookRecord::PrintRecord()
{
    cout << static_cast<const char*>(m_sName) << endl;
    cout << "Stock Number: " << m_lStockNum << endl;
    cout << "Class: " << m_iClassification << endl;
    cout << "Cost: $" << m_dCost << endl;
    cout << "In Stock: " << m_iCount << endl;
}

If you are wondering about the exception throwing have a look at this post.

Hope it helps

Community
  • 1
  • 1
tiki
  • 141
  • 1
  • 12