Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 4th, 2007, 02:28 PM
Registered User
 
Join Date: May 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default run-time error(s)

hey there,

I was wondering if couild tell me what was wrong with the following pieces of code. it compiled error-free but during run-time it reported few errors. below is the source code:

/*Box.h*/

#ifndef BOX_H
#define BOX_H

class Box
{
public:

    Box(double Length = 1.0, double Breadth = 1.0, double Height = 1.0);

    double volume();

    double getLength();
    double getBreadth();
    double getHeight();

    int compareVolume(Box &otherBox) ;

private:

    double length;
    double breadth;
    double height;

};

#endif

                                        x--------x

/*List.h*/

#ifndef LIST_H
#define LIST_H

#include "Box.h"

class Package
{
public:

    Package(Box *pNewBox);
    Box *getBox();
    Package *getNext();
    void setNext(Package *pPackage);

private:

    Box *pBox;
    Package *pNext;

};

class TruckLoad
{
public:

    TruckLoad();
    TruckLoad(Box *pBox, int count);

    Box *getFirstBox();
    Box *getNextBox();
    void addBox(Box *pBox);

private:

    Package *pHead;
    Package *pTail;
    Package *pCurrent;
};

#endif

                                        x--------x

/*Box.cpp*/

#include "Box.h"

Box::Box(double length, double breadth, double height)
{
    length = length>0.0?length:1.0;
    breadth = breadth>0.0?breadth:1.0;
    height = height>0.0?height:1.0;
}

double Box::volume()
{
    return length*breadth*height;
}

double Box::getLength()
{
    return length;
}

double Box::getBreadth()
{
    return breadth;
}

double Box::getHeight()
{
    return height;
}

int Box::compareVolume(Box &otherBox)
{
    double vol1 = volume();
    double vol2 = otherBox.volume();
    return vol1>vol2?1:(vol1<vol2?-1:0);
    return 0;
}

/*CConApp95.cpp*/

                                        x--------x

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

#include "Box.h"
#include "List.h"

inline int random(int count)
{
    return 1+static_cast<int>((count*static_cast<long>(rand() ))/(RAND_MAX+1));
}

int main()
{
    const int dimLimit = 100;

    TruckLoad load1;

    for(int i=0;i<10;i++)
    {
        load1.addBox(new Box(random(dimLimit), random(dimLimit), random(dimLimit)));
    }

    Box *pBox = load1.getFirstBox();
    Box *pNextBox;
    while(pNextBox = load1.getNextBox())
    {
        if(pBox->compareVolume(*pNextBox)<0)
        {
            pBox = pNextBox;
        }
    }

    cout<<endl
        <<"The largest box in the first list is "
        <<pBox->getLength()<<" by "
        <<pBox->getBreadth()<<" by "
        <<pBox->getHeight()<<endl;

    const int boxCount = 20;

    Box boxes[boxCount];

    for(int i=0;i<boxCount;i++)
    {
        boxes[i] = Box(random(dimLimit), random (dimLimit), random(dimLimit));
    }

    TruckLoad load2(boxes, boxCount);

    pBox = load2.getFirstBox();
    while(pNextBox = load2.getNextBox())
    {
        if(pBox->compareVolume(*pNextBox)<0)
        {
            pBox = pNextBox;
        }
    }

    cout<<endl
        <<"The largest box in the second list is "
        <<pBox->getLength()<<" by "
        <<pBox->getBreadth()<< " by "
        <<pBox->getHeight()<<endl;

    pNextBox = load1.getFirstBox();

    while(pNextBox)
    {
        delete pNextBox;
        pNextBox = load1.getNextBox();
    }

    return 0;
}

/*List.cpp*/

                                        x--------x

#include "Box.h"
#include "List.h"

Package::Package(Box *pNewBox)
{
}

Box *Package::getBox()
{
    return pBox;
}

Package *Package::getNext()
{
    return pNext;
}

void Package::setNext(Package *pPackage)
{
    pNext = pPackage;
}

TruckLoad::TruckLoad(Box *pBox, int count)
{
    pHead = pTail = pCurrent = 0;

    if((count>0)&&(pBox!=0))
    {
        for(int i=0;i<count;i++)
        {
            addBox(pBox+i);
        }
    }
}

TruckLoad::TruckLoad()
{d
}

Box *TruckLoad::getFirstBox()
{
    pCurrent = pHead;
    return pCurrent->getBox();
}

Box *TruckLoad::getNextBox()
{
    if(pCurrent)
        pCurrent = pCurrent->getNext();
    else
        pCurrent = pHead;

    return pCurrent?pCurrent->getBox():0;
}

void TruckLoad::addBox(Box *pBox)
{
    Package *pPackage = new Package(pBox);

    if(pHead)
        pTail->setNext(pPackage);
    else
        pHead = pPackage;
    pTail = pPackage;
}


Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Run-time error '2450' bmurrin Access VBA 6 May 31st, 2006 07:11 AM
run time error ashishroyk Java GUI 0 October 8th, 2004 01:42 AM
Run Time Error JBond Access VBA 0 May 27th, 2004 09:50 AM
RUN-TIME ERROR compcad Beginning VB 6 2 May 21st, 2004 02:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.