### Help with a program

Ok I'm new here and I thought I can get some help with a program I'm working on.
First let me give you an idea of what the program is about. I have to write a program that calculates shipping charges for orders. Using two dimensional array where all orders are charged 20.00 plus an additional rate based on quantity and region. Here's my code:

#include <iostream>
#include <iomanip>
#include <vector>
#define HEIGHT 6
#define WIDTH 6
using namespace std;
int main()
{
double **p2DArray;

p2DArray = new double*[HEIGHT];
for (int i = 0; i < HEIGHT; ++i)
p2DArray[i] = new double[WIDTH];

//0 = quantity 1
p2DArray[0][0] = 1.00;
p2DArray[0][1] = 2.00;
p2DArray[0][2] = 3.00;
p2DArray[0][3] = 4.00;
p2DArray[0][4] = 5.00;
//1 = quantity 2
p2DArray[1][0] = 2.00;
p2DArray[1][1] = 4.00;
p2DArray[1][2] = 6.00;
p2DArray[1][3] = 8.00;
p2DArray[1][4] = 10.00;
//2 = quantity 3
p2DArray[2][0] = 2.50;
p2DArray[2][1] = 4.50;
p2DArray[2][2] = 6.50;
p2DArray[2][3] = 8.50;
p2DArray[2][4] = 10.50;
//3 = quantity 4
p2DArray[3][0] = 3.00;
p2DArray[3][1] = 5.00;
p2DArray[3][2] = 7.00;
p2DArray[3][3] = 9.00;
p2DArray[3][4] = 11.00;
//4 = quantity 5
p2DArray[4][0] = 3.25;
p2DArray[4][1] = 5.25;
p2DArray[4][2] = 7.25;
p2DArray[4][3] = 9.25;
p2DArray[4][4] = 11.25;
//5 = quantity >5
p2DArray[5][0] = 3.50;
p2DArray[5][1] = 5.50;
p2DArray[5][2] = 7.50;
p2DArray[5][3] = 9.50;
p2DArray[5][4] = 11.50;

for (int i = 0; i < HEIGHT; ++i)
delete [] p2DArray[i];
delete [] p2DArray;

int quantity, region;
double addRate, totalShip, basicShip = 20.00;
cout << "Enter the destination shipping region: ";
cin>>region;
cout << "Enter the quantity of items shipped: ";
cin>>quantity;

if (region == 1)
{
if (quantity == 1)
{
}
if (quantity == 2)
{
}
if (quantity == 3)
{
}
if (quantity == 4)
{
}
if (quantity == 5)
{
}
else
{
}
}

if (region == 2)
{
if (quantity == 1)
{
}
if (quantity == 2)
{
}
if (quantity == 3)
{
}
if (quantity == 4)
{
}
if (quantity == 5)
{
}
else
{
}
}

if (region == 3)
{
if (quantity == 1)
{
}
if (quantity == 2)
{
}
if (quantity == 3)
{
}
if (quantity == 4)
{
}
if (quantity == 5)
{
}
else
{
}
}
if (region == 4)
{
if (quantity == 1)
{
}
if (quantity == 2)
{
}
if (quantity == 3)
{
}
if (quantity == 4)
{
}
if (quantity == 5)
{
}
else
{
}
}

else
{
if (quantity == 1)
{
}
if (quantity == 2)
{
}
if (quantity == 3)
{
}
if (quantity == 4)
{
}
if (quantity == 5)
{
}
else
{
}
}

totalShip = basicShip + addRate;
cout<<"The basic shipping rate = "<<basicShip<<" . "<<endl;
cout<<"Shipping "<<quantity<<" items to shipping-destination region "<<region<<" the additional rate is "<<addRate<<" . "<<endl;
cout<<"The total shipping rate = "<<totalShip<<" . "<<endl;

return 0;
}

It builds/compiles successfully but when I run the program and insert the values. I get 11.50 for everything. When I enter 1 quantity and region 1 it should be 20 + 1. But it is not, what am I doing wrong? I need a quick reply, please help.
First of all, please use code tags (`[code]`[/code]) in the future to make your posted code easier to read.

Secondly, why are you deleting your array before you access it?

Correct that and I think you'll see your program functioning much better.
Last edited on
Topic archived. No new replies allowed.