Thread: Output

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

    Output

    I'm wondering why I can't output ounces on the screen in c#
    here's the code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter7Problem12
    {
        class Program
        {
    
            static void Main()
            {
                int numOfOrders = 0;
                int count = 1;
                int ounces = 0;
                double[] array = new double[numOfOrders];
    
    
                Console.Write("\nEnter number of order: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
              
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\nEnter Weight: ");
                    ounces = Convert.ToInt32(Console.ReadLine());
                   
                    if (ounces <= 8)
                        array[i] = 1.50;
                    else if ((ounces >= 8) && (ounces < 32))
                        array[i] = 1.50 + (.50 * (ounces - 8));
                    else if (ounces >= 32)
                        array[i] = 13.50 + (.75 * (ounces - 32));
    
                }
    
              
    
                Console.Write("\n# Of Orders     Weight               Shipping Charges");
                Console.Write("\n-----------------------------------------------------");
    
    
    
    
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\n{0,11}{1,11}{2,31:C}\n", count++, ounces, array[i]);
                }
    
                Console.ReadLine();
    
            }
        }
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    double[] array = new double[numOfOrders];
    You are creating array of 0 elements. What do you expect will happen when you access a member that is not there?
    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
    Ok I see what you mean but it still doesn't fix my problem because the array[i] is correct its the ounces variable that I can't get it to output it just prints the last number when I input the numOfOrders here's the code adjustment that I made:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter7Problem12
    {
        class Program
        {
    
            static void Main()
            {
                int numOfOrders = 0;
                int count = 1;
                int ounces = 0;
              
    
    
                Console.Write("\nEnter number of order: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
    
                double[] array = new double[numOfOrders];
    
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\nEnter Weight: ");
                    ounces = Convert.ToInt32(Console.ReadLine());
                   
                    if (ounces <= 8)
                        array[i] = 1.50;
                    else if ((ounces >= 8) && (ounces < 32))
                        array[i] = 1.50 + (.50 * (ounces - 8));
                    else if (ounces >= 32)
                        array[i] = 13.50 + (.75 * (ounces - 32));
    
                }
    
              
    
                Console.Write("\n# Of Orders     Weight               Shipping Charges");
                Console.Write("\n-----------------------------------------------------");
    
    
    
    
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\n{0,11}{1,11}{2,31:C}\n", count++, ounces, array[i]);
                }
    
                Console.ReadLine();
    
            }
        }
    }
    
    I don't know if this is the correct way to fix the problem if you can give me a hint that would be great

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I do not know what you store into array - but you store numOfOrders values in it.
    On the other hand ounces - you have only 1 variable and so you store in it last read value.

    If you expect to output all read values - you need to store them all, so it means - you need to use another array.

    PS. Ah, based on the output title - I see array shoudl store "Shipping Charges" - so array is not good name - better use something like

    Code:
    double shippingCharges[];
    Last edited by vart; 06-25-2014 at 10:32 PM.
    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
    I changed the array[i] name to charges[i] which is better. I understand that I need to make ounces an array. I just don't know how to do it I know how to declare an array I don't know how to assign it to Console.ReadLine without it causing errors

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter7Problem12
    {
        class Program
        {
    
            static void Main()
            {
                int numOfOrders = 0;
                int count = 1;
               
              
    
    
                Console.Write("\nEnter number of order: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
    
                int[] weight = new int[numOfOrders];
                double[] charges = new double[numOfOrders];
    
                for (int i = 1; i < numOfOrders; i++)
                {
                    Console.Write("\nEnter Weight: ");
                    numOfOrders  = Convert.ToInt32(Console.ReadLine());
                   
                    if(weight[i] <= 8)
                        charges[i] = 1.50;
                    else if ((weight[i] >= 8) && (weight[i] < 32))
                        charges[i] = 1.50 + (.50 * (weight[i] - 8));
                    else if (weight[i] >= 32)
                        charges[i] = 13.50 + (.75 * (weight[i] - 32));
    
                }
    
              
    
                Console.Write("\n# Of Orders     Weight               Shipping Charges");
                Console.Write("\n-----------------------------------------------------");
    
    
    
    
                for (int i = 1; i < numOfOrders; i++)
                {
                    Console.Write("\n{0,11}{1,11}{2,31:C}\n", count++, weight[i], charges[i]);
                }
    
                Console.ReadLine();
    
            }
        }
    }
    Ok, I got this far but I get an index out of bounds error with this code snip


    if(weight[i] <= 8)

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

    You gave me just enough to figure it out thanks

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter7Problem12
    {
        class Program
        {
    
            static void Main()
            {
                int numOfOrders = 0;
                int count = 1;
               
                Console.Write("\nEnter number of order: ");
                numOfOrders = Convert.ToInt32(Console.ReadLine());
    
                int[] weight = new int[numOfOrders];
                double[] charges = new double[numOfOrders];
    
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\nEnter Weight: ");
                    weight[i]  = Convert.ToInt32(Console.ReadLine());
                   
                    if(weight[i] <= 8)
                        charges[i] = 1.50;
                    else if ((weight[i] >= 8) && (weight[i] < 32))
                        charges[i] = 1.50 + (.50 * (weight[i] - 8));
                    else if (weight[i] >= 32)
                        charges[i] = 13.50 + (.75 * (weight[i] - 32));
    
                }
    
              
    
                Console.Write("\n# Of Orders     Weight               Shipping Charges");
                Console.Write("\n-----------------------------------------------------");
    
    
    
    
                for (int i = 0; i < numOfOrders; i++)
                {
                    Console.Write("\n{0,11}{1,11}{2,31:C}\n", count++, weight[i], charges[i]);
                }
    
                Console.ReadLine();
    
            }
        }
    }
    I think this code works I figured out what I did wrong thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying input to output K&R there is no output
    By amadeus111 in forum C Programming
    Replies: 5
    Last Post: 02-24-2013, 05:09 AM
  2. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  3. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  4. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM