ok so im working on a application that displays the charges along with displaying the zip code. I seem to have a IO exception error for the console.clear() line. Is there anything else I need in order to display the charges along with zip code? Im thinking a nested if statement possibly?

Thanks a lot everyone

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PackageDelivery
{
    class Program
    {
        static void Main(string[] args)
        {
            int zipThatIamLookingFor;
            string inValue;
            double charges; 
            bool moreData = true;

            int[] zipCode = new int[10] 
                { 32309, 32398, 32345, 32211, 
                    32215, 32312, 32317, 32308, 32356, 32349 };

            double[] deliveryCharges = new double[10] { 10.00, 12.00, 20.00, 1.00, 5.00,
                7.00, 2.00, 50.00, 80.00, 75.00 };

            while (moreData == true)
            {
                //this recieves the zip code from the user and stores 
                //in method 
                zipThatIamLookingFor = GetZipCode();
                //this test the zip code entered by user 
                Test(zipCode, zipThatIamLookingFor); 
                //method to display the charges
                
                //displays message 
                 Console.Write("Do you want to enter another zip?");
                //takes in user input 
                inValue = Console.ReadLine();
                //takes both upper/lower case letters 
                if (inValue == "y" || inValue == "Y")
                    //test the response through if..else loops 
                    moreData = true;
                else
                    moreData = false;
                //displays message if bool value = false 
                Console.WriteLine("Sorry we currently do not deliever to your area."); 
            } // end of while loop 





        }

        static int GetZipCode()
        {
            int searchValue;
            string inValue;

            Console.WriteLine("Test Zipcode: ");
            inValue = Console.ReadLine();
            searchValue = Convert.ToInt32(inValue);
            return searchValue;
        } // end of GetZipCode 

        static double DisplayCharges()
        {
            //initializing so compilier does not report unassigned local
            //variable
            double charges = 0; 
            Console.WriteLine("Price for shipping for your zipcode: ");
            return charges;

        } // end of displayCharges 

        static void Test(int[] zipCode, int searchValue)
        {
            bool isItThere = false;
            for (int i = 0; i < zipCode.Length; i++)
            {

                if (zipCode[i] == searchValue)
                {
                    isItThere = true;
                    i = zipCode.Length;
                } // end of if loop 

            } // end of for loop 

            //IO exception unhandled
            Console.Clear();

            if (isItThere == true)
                Console.WriteLine("Zip is in the delivery area!");
            else
                Console.WriteLine("Zip is not in the delivery area!");

        } // end of Test method 

    } //end of class 
} // end of namespace

Recommended Answers

All 6 Replies

I ran this and did not get an error.
I would suggest a serious restructuring of the code, however.

If you're allowed or accustomed, I would recommend the use of a Dictionary<string, double> to allow quick lookups without all of the looping.

var map_s2dZipCharges = new Dictionary<string,double>
         {
            {"32309", 10.00},
            {"32398", 12.00},
            {"32345", 20.00},
            {"32211", 1.00},
            {"32215", 5.00},
            {"32312", 7.00},
            {"32317", 2.00},
            {"32308", 50.00},
            {"32356", 80.00},
            {"32349", 75.00}
         };
// and you can search it by doing a ContainsKey()
//... //\\//\\//\\//\\

            if (map_s2dZipCharges.ContainsKey(strSelection))
            {
               Console.WriteLine("It costs ${0} to deliver to {1}",
                  map_s2dZipCharges[strSelection],
                  strSelection);
            }
//...

Also, of you're going to keep the two arrays, I would recommend a function that simply returns a bool that denotes whether the value was found (or not) and do not write to the disoplay inside that function.

thanks it pointed me in the right direction I have modified the code a bit with some that you posted but I keep getting this annoying problem of unassigned local variable, if I declare a memory location then i get a parent meaning has been defined in the scope and would give a different meaning...Im stuck.. for lines 81-85

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PackageDelivery
{
    class PackageDeliveryCharge
    {
        static void Main(string[] args)
        {
            int zipThatIamLookingFor;
            string inValue;
            double charges;
            bool moreData = true;


            int[] zipCode = new int[10] 
                { 32309, 32398, 32345, 32211, 
                    32215, 32312, 32317, 32308, 32356, 32349 };

            double[] deliveryCharges = new double[10] { 10.00, 12.00, 20.00, 1.00, 5.00,
                7.00, 2.00, 50.00, 80.00, 75.00 };

            while (moreData == true)
            {
                zipThatIamLookingFor = GetZipCode();
                Test(zipCode, zipThatIamLookingFor);
                Console.Write("Do you want to enter another zip?");
                inValue = Console.ReadLine();
                if (inValue == "y" || inValue == "Y")
                    moreData = true;
                else
                    moreData = false;
            } // end of while loop 
         
       }// end of main 

        static int GetZipCode()
        {
            int searchValue;
            string inValue;

            Console.WriteLine("Please enter ZipCode: ");
            inValue = Console.ReadLine();
            searchValue = Convert.ToInt32(inValue);
            return searchValue;
        } // end of GetZipCode 

        static double DisplayCharges()
        {
            string usrSelection;
            
            double charges = 0;
            Console.WriteLine("You charges for requested zip code: ");

            var ZipCharges = new Dictionary<string, double>
            {
                {"32309", 10.00},

                {"32398", 12.00},

                {"32345", 20.00},

                {"32211", 1.00},

                {"32215", 5.00},

                {"32312", 7.00},

                {"32317", 2.00},

                {"32308", 50.00},

                {"32356", 80.00},

                {"32349", 75.00}

            }; // end of Dictionary lookoup 

            if (ZipCharges.ContainsKey(usrSelection))
            {
                
                Console.WriteLine("It will cost ${0} to deliver to {1}", ZipCharges[usrSelection], usrSelection);
            }

            return charges;
 
        } // end of DisplayCharges 


        static void Test(int[] zipCode, int searchValue)
        {
            bool isItThere = false;
            for (int i = 0; i < zipCode.Length; i++)
            {

                if (zipCode[i] == searchValue)
                {
                    isItThere = true;
                    i = zipCode.Length;
                } // end of if loop 

            } // end of for loop 
            Console.Clear();

            if (isItThere == true)
                Console.WriteLine("Zip is in the delivery area!");
            else
                Console.WriteLine("Zip is not in the delivery area!");
            
        } // end of Test method 

    } // end of class 
} // end of namespace

thanks as always

Right, you have not yet gotten the user response INTO the usrSelection variable, so it is uninitialized when you attempt to compare it to the dictionary keys.

After that, you still have in a lot of code that will be unused.

ok tweaked it a bit more:

I now need to adjust something at line 57 where the console will grab the dictionary method value..If i put "null" (totally wrong to do) it displays line 59 nevertheless..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PackageDelivery
{
    class PackageDeliveryCharge
    {
        static void Main()
        {
            int zipThatIamLookingFor;
            string inValue;
            double showCharge;
            string usr;
            bool moreData = true;


            int[] zipCode = new int[10] 
                { 32309, 32398, 32345, 32211, 
                    32215, 32312, 32317, 32308, 32356, 32349 };

            double[] deliveryCharges = new double[10] { 10.00, 12.00, 20.00, 1.00, 5.00,
                7.00, 2.00, 50.00, 80.00, 75.00 };

            while (moreData == true)
            {
                zipThatIamLookingFor = GetZipCode();
                showCharge =  DisplayCharges();
                Test(zipCode, zipThatIamLookingFor, showCharge);
                Console.Write("Do you want to enter another zip?");
                inValue = Console.ReadLine();
                if (inValue == "y" || inValue == "Y")
                    moreData = true;
                else
                    moreData = false;
            } // end of while loop 
         
       }// end of main 

        static int GetZipCode()
        {
            int searchValue;
            string inValue;
            

            Console.WriteLine("Please enter ZipCode: ");
            inValue = Console.ReadLine();
            searchValue = Convert.ToInt32(inValue);
            return searchValue;
        } // end of GetZipCode 

        static double DisplayCharges()
        {   
            //string 'usr' declares usr variable in inner scope 'if" and allows child scope to access parent scope data without
            //overlap 
            string usr = ;
            double charges = 0;
            Console.WriteLine("You charges for requested zip code: ");
           
            var ZipCharges = new Dictionary<string, double>
            {
                {"32309", 10.00},

                {"32398", 12.00},

                {"32345", 20.00},

                {"32211", 1.00},

                {"32215", 5.00},

                {"32312", 7.00},

                {"32317", 2.00},

                {"32308", 50.00},

                {"32356", 80.00},

                {"32349", 75.00}

            }; // end of Dictionary lookoup 

            //this later scope includes block of code in the previous "outer" scope . So inner block containing "earlier" variable 
            //cant refer to later variable in a place earlier than its declaration. 
            if (ZipCharges.ContainsKey(usr))
            {
                
                //declares 'input' identifiers for variables on the writeline statement 
                string input= ""; 
                Console.WriteLine("It will cost ${0} to deliver to {1}", ZipCharges[input], input);
                
            }
            
            return charges;
 
        } // end of DisplayCharges 


        static void Test(int[] zipCode, int searchValue, double showCharge)
        {
            bool isItThere = false;
            for (int i = 0; i < zipCode.Length; i++)
            {

                if (zipCode[i] == searchValue)
                {
                    isItThere = true;
                    i = zipCode.Length;
                } // end of if loop 

            } // end of for loop 
            Console.Clear();

            if (isItThere == true)
                Console.WriteLine("Zip is in the delivery area!");
            else
                Console.WriteLine("Zip is not in the delivery area!");
            
        } // end of Test method 

    } // end of class 
} // end of namespace

Either remove "usr" or "input".
Which ever one is left use that to receive the value from the Console.ReadLine().

Remove the Test() method.

Remove the zipCode array.
Remove the charges array.

Put the dictionary inside Main and remove the DisplayCharges() method.

This thread is getting a bit painful to follow. You are making your program WAY overcomplicated for what it is doing. If this is for homework, please try to understand the following code before rather than just copy it and hand it in.

static Dictionary<string, double> _ZipsAndCharges = new Dictionary<string, double>
        {                
            {"32309", 10.00},
            {"32398", 12.00},
            {"32345", 20.00},
            {"32211", 1.00},
            {"32215", 5.00},
            {"32312", 7.00},
            {"32317", 2.00},
            {"32308", 50.00},
            {"32356", 80.00},
            {"32349", 75.00}
        }; 
        static void Main()
        {
            string ZipCode = "";

            Console.Write("Zip Code Charge Finder" + Environment.NewLine + "Enter Zip Code? (Y/N) ");
            while (char.ToLower(Console.ReadKey(true).KeyChar) == 'y')
            {
                Console.Write(Environment.NewLine + "Enter Zip: ");
                ZipCode = Console.ReadLine().Trim;
                if (_ZipsAndCharges.ContainsKey(ZipCode))
                    Console.WriteLine(string.Format("Charges for zip code {0} = {1}", ZipCode, _ZipsAndCharges[ZipCode]));
                else
                    Console.WriteLine(string.Format("Charges for zip code {0} not found", ZipCode));
                Console.Write("Look up another zip code? (Y/N) ");
            }
        }
commented: Timely +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.