Hello all.

A friend is shipping some items in a container so it gave me the inspiration to write a quick little program. This only took 20minutes at best so please bear that in mind.

The basic idea is if the containers size is known, and the cost is known then the program will work out the cost of an individual item providing you know its dimensions.

As I'm very much a C++ beginner would anyone be able to criticize the layout of the program or in fact its functionality? Also if my maths is wrong anywhere please point it out.

Thank you in advance!

//Basic program to calculate the shipping cost of an individual item on a container.
//Program assumes you will make use of every cubic metre
//Program uses pre-defined values for total container cost and size of container.

#include <iostream>
using namespace std;

double convertToM(double nSize)
    return nSize / 100; //Convert value from CM to M

double convertToCubicMetres(double Length, double Width, double Height)
    return Length * Width * Height; //Calculate items size in Cubic Metres

int main()
    const double containerCost = 3000.00; //cost of 40ft container
    const double containerSize = 67.11; //size of container in cubic metres

    double itemLength = 0.0;
    double itemWidth = 0.0;
    double itemHeight = 0.0;

    int again = 0;


    //Currently assumes input is "Number Number Number" ie: 100 100 100. 
    cout << "Please enter the size of your item (Length x Width x Height) in CM." << endl << endl;
    cin >> itemLength;
    cin >> itemWidth;
    cin >> itemHeight;

    itemLength = convertToM(itemLength);
    itemWidth = convertToM(itemWidth);
    itemHeight = convertToM(itemHeight);

    double itemCubicMetres = 0.0;
    itemCubicMetres = convertToCubicMetres(itemLength, itemWidth, itemHeight);
    cout << "Your item is: " << itemCubicMetres << " Cubic Metres.";

    double itemCost = 0.0;
    itemCost = (containerCost / containerSize) * itemCubicMetres;

    cout << endl << endl;
    cout << "Your item would cost: " << (char)156 << itemCost << " to ship.";

    cout << endl << endl;
    cout << "Would you like to calculate the cost of another item? ((1) Yes (2) No)?";
    cin >> again;

    cout << endl << endl;

    } while (again == 1);

    return 0;

This code looks fine to me. If you wrap it into classes it could be even better, then you could probably add functionality for fitting the objects into the container (since usually not every square meter of the container is filled). I would suggest a containerItem class and a container class like this:

struct containerItem
    double width,height,length;
class container
    containerItem dimensions;
    container(containerItem dimensions):dimensions(dimensions){}
    double calculateCost(containerItem i);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.