Output

This is a discussion on Output within the C# Programming forums, part of the General Programming Boards category; 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; ...

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

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    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, 04: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, 09:46 PM
  5. Replies: 3
    Last Post: 02-19-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21