Thread: index out of bounds error

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    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: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
    
                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: ");
                    orders = Convert.ToDouble(Console.ReadLine());
    
                    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. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    when you create array with numOfOrders elements valid indexes are from 0 to numOfOrders-1

    numOfOrders index is invalid and access to member Order[numOfOrders] will cause your mentioned error
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    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

    Your code shoudl be
    Code:
    for(i = 0; i < numOfOrders; i++)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
    
                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: ");
                    Order[i] = Convert.ToDouble(Console.ReadLine());
    
                    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);
    
                Console.ReadLine();
           }
       }
    }

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  2. Replies: 5
    Last Post: 01-16-2012, 02:22 PM
  3. Replies: 1
    Last Post: 08-12-2011, 03:07 AM
  4. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM
  5. illegal vector index and checking bounds
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2001, 11:36 AM