# Thread: index out of bounds error

1. ## index out of bounds error

Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter7Problem13
{
class Program
{
static void Main()
{
int i;
int numOfOrders;
double orders = 0.00;
double discount = 0.00;
double nPrice = 0.00;

double totalOrders = 0;
double totalDiscount = 0;
double totalnetNetPrice = 0;

Console.Write("\nEnter # of Orders: ");

double[] Order = new double[numOfOrders];
double[] Discount = new double[numOfOrders];
double[] netPrice = new double[numOfOrders];

for (i = 0; i <= numOfOrders; i++)
{
Console.Write("\nEnter Total Order: ");

if (orders <= 200.00)
discount = .10 * orders;
if (orders > 200.00)
discount = .15 * orders;
}

nPrice = orders - discount;

Console.Write("\n      ORDERS              DISCOUNT          NET PRICE\n");
Console.Write("-------------------------------------------------------\n");

totalOrders += orders;
totalDiscount += discount;
totalnetNetPrice += nPrice;

for(i = 0; i <= numOfOrders; i++)
{

Console.Write("\n{0,14:C}{1,22:C}{2,19:C}\n", Order[i], Discount[i], netPrice[i]);
Console.Write("-------------------------------------------------------\n");
Console.Write("\n{0,14:C}{1,22:C}{2,19:C}\n", totalOrders, totalDiscount, totalnetNetPrice);
}
}
}
}```
I keep getting an out of bounds error in my code

I'm suppose to enter number of orders then enter those orders then calculate a discount and net price display the orders, discount, net price then the totals at the bottom If someone can figure it out because I'm totally frustrate with this

2. when you create array with numOfOrders elements valid indexes are from 0 to numOfOrders-1

3. I don't mean to be so stupid but, I don't understand what you mean why is numOfOrders invalid I assign it a value when it asks I think Order[i] array doesn't have any values in it I don't know how to assign the orders variable to it or is my approach totally wrong

4. Code:
`for(i = 0; i <= numOfOrders; i++)`
you are using values from 0 to numOfOrders included for i, you can only use values from 0 to numOfOrders-1

Code:
`for(i = 0; i < numOfOrders; i++)`

5. Hey man thanks you fixed my code It is working great now
here it is

Code:
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter7Problem13
{
class Program
{
static void Main()
{

int numOfOrders;

double totalOrders = 0.00;
double totalDiscount = 0.00;
double totalNetPrice = 0.00;

Console.Write("\nEnter # of Orders: ");

double[] Order = new double[numOfOrders];
double[] Discount = new double[numOfOrders];
double[] netPrice = new double[numOfOrders];

for (int i = 0; i < numOfOrders; i++)
{
Console.Write("\nEnter Total Order: ");

if (Order[i] <= 200.00)
Discount[i] = .10 * Order[i];
if (Order[i] > 200.00)
Discount[i] = .15 * Order[i];

netPrice[i] = Order[i] - Discount[i];

totalOrders += Order[i];
totalDiscount += Discount[i];
totalNetPrice += netPrice[i];

}

Console.Write("\n      ORDERS              DISCOUNT          NET PRICE\n");
Console.Write("-------------------------------------------------------\n");

for(int i = 0; i < numOfOrders; i++)
{
Console.Write("\n{0,12:C}{1,22:C}{2,19:C}\n", Order[i], Discount[i], netPrice[i]);
}
Console.Write("\n-------------------------------------------------------");

Console.Write("\n{0,12:C}{1,22:C}{2,19:C}", totalOrders, totalDiscount, totalNetPrice);

}
}
}```

6. Oh, by the way I understand the concept now I didn't at first it's actually fairly simple I can't believe I didn't see that before thanks again