Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 4th, 2007, 02:28 PM
Registered User
 
Join Date: May 2004
Location: gdfgdf, gdf, Afghanistan.
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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 04:28 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.