Click here to Skip to main content
15,889,648 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I keep getting two errors on the performMenueOption() inputs
for both lengthInMeters and widthInMeters i get this error,
Error 1 Use of unassigned local variable 'lengthInMeters'
Error 2 Use of unassigned local variable 'widthInMeters'
can someone please help me out.
thanks (sorry for code dumping)

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

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

            int menuOption;
            double lengthInMeters;
            double widthInMeters;
            do
            {
                MainMenu();
                menuOption = ReadOption();
                PerformMenuOption(menuOption, lengthInMeters, widthInMeters);


            } while (menuOption != 4);

        }
        /// Displays Main menu
        /// precondition: true
        /// postcondition: displays the main menu to the screen
        public static void MainMenu()
        {
            string mainMenu = "\n\n=======================================================================\n"
                            + "\n1) Calculate cost of carpeting a room with particular carpet\n"
                            + "2) Compare cost of carpeting a room with two different types of carpet\n"
                            + "3) Enter/Change room dimensions\n"
                            + "4) Exit\n"
                            + "Please enter your selection 1-4\n\n"
                            + "========================================================================\n";

            Console.Write(mainMenu);
        }// end MainMenu

        /// Reads users input
        /// Preconditions: true
        /// Postcondition:returns options which is either 1,2,3 or 4.
        static int ReadOption()
        {

            int option;
            bool ValidMenuOption = false;

            do
            {
                while (!Int32.TryParse(Console.ReadLine(), out option))
                {
                    Console.WriteLine("Please try again with a valid menu option.");
                }

                if ((1 <= option)
                    && (option <= 4))
                {
                    ValidMenuOption = true;
                }
                else
                {
                    ValidMenuOption = false;
                } //end if

                if (!ValidMenuOption)
                {
                    Console.WriteLine("\n\t You have entered an invalid option please enter 1, 2, 3 or 4");
                    MainMenu();
                } //end if
            } while (!ValidMenuOption);

            return option;
        }//end ReadOption


        /// reads dimensions of the room
        /// Precondition: true
        /// Pastcondition: returns the cost of carpeting the room.????

        static void PerformMenuOption(int option, double lengthInMeters, double widthInMeters)
        {

            double value;
            if (option == 1)
            {

                int carpetMenuOption;
                bool dimensions = DimensionsEntered(lengthInMeters, widthInMeters);
                do
                {
                    bool previousDimensions;
                    previousDimensions = DimensionsEntered(lengthInMeters, widthInMeters);
                    if (previousDimensions == false)
                    {
                       ReadLength();
                       ReadWidth();

                    }

                    CarpetMenu();
                    carpetMenuOption = ReadCarpetOption();

                    value = CalculateCarpetCost(lengthInMeters, widthInMeters, carpetMenuOption);


                } while (carpetMenuOption != 5);

                MainMenu();


            }
            else if (option == 2)
            {
             // to be compleated
            }
            else if (option == 3)
            {
            // to be compleated

            }
            else
            {
                // need to complete
                DateTime date = DateTime.Now;
                Console.WriteLine("\n Today's Date and Time: {0}\n", date);


            }
        }

        /// Reads length of room
        /// Preconditions:true
        /// Postconditions:returns length in meters
        static double ReadLength()
        {
            double lengthInMeters;

            do
            {
                Console.Write("Please eneter the length of the room you wish to carpet in meters:");

                while (!double.TryParse(Console.ReadLine(), out lengthInMeters))
                {
                    Console.WriteLine("Please try again with a valid length option.");
                }

                if (lengthInMeters < 2.4 || lengthInMeters > 4.5)
                {
                    Console.Write("\nYou have entered an illegal length, the dimensions of the room must not be larger than 4.5m or smaller than 2.4m."
                            + "\n Please enter the length of your room in meters again");
                }//end if

            } while (lengthInMeters < 2.4 || lengthInMeters > 4.5);
            return lengthInMeters;
        }// end ReadLength


        /// Reads Width of room
        /// Preconditions: true
        /// Postconditions: returns the width of the room
        static double ReadWidth()
        {
            double widthInMeters;

            do
            {
                Console.Write("Please eneter the width of the room you wish to carpet in meters:");
                //  widthInMeters = double.Parse(Console.ReadLine());

                while (!double.TryParse(Console.ReadLine(), out widthInMeters))
                {
                    Console.WriteLine("Please try again with a valid width option.");
                }


                if (widthInMeters < 2.4 || widthInMeters > 4.5)
                {
                    Console.Write("\nYou have entered an illegal width, the dimensions of the room must not be bigger then 4.5m or smaller than 2.4m."
                            + "\n please enter the width of your room in meters again");
                }//end if

            } while (widthInMeters < 2.4 || widthInMeters > 4.5);
            return widthInMeters;

        }//end ReadWidth

        /// checks to see if dimensions have been entered
        /// Preconditions: false
        /// postconditions:returns haveDimensionsBeenEntered
        public static bool DimensionsEntered(double lengthInMeters, double widthInMeters)
                    {

            bool haveDimensionsBeenEntered = false;

            if (lengthInMeters > 0 && widthInMeters > 0)
            {
                haveDimensionsBeenEntered = true;
            }
            return haveDimensionsBeenEntered;
        }//end dimensions entered



        /// Displays
        /// precondition: true
        /// postcondition: displays carpet menu
        public static void CarpetMenu()
        {
            string carpetMenu = "\n\n"
                            + "\t1) Berber $39.95 per meter\n"
                            + "\t2) Frieze $34.95 per meter\n"
                            + "\t3) Pile $23.59 per metern\n"
                            + "\t4) Plush $19.99 per meter\n"
                            + "\t5) Exit\n"
                            + "Please enter your selection 1-5\n";

            Console.Write(carpetMenu);
        }// end CarpetMenu


        /// Reads users input
        /// Preconditions: true
        /// Postcondition:returns options which is either 1,2,3 or 4.
        static int ReadCarpetOption()
        {
            int carpetOption = 0;
            bool ValidCarpetOption = false;

            do
            {

                while (!Int32.TryParse(Console.ReadLine(), out carpetOption))
                {
                    Console.WriteLine("Please try again with a valid menu option.");
                }

                if ((1 <= carpetOption)
                    && (carpetOption <= 5))
                {
                    ValidCarpetOption = true;
                }
                else
                {
                    ValidCarpetOption = false;
                } //end if
                if (!ValidCarpetOption)
                {
                    Console.WriteLine("\n\t Option must be 1, 2, 3, 4 or 5");
                    CarpetMenu();
                } //end if
            } while (!ValidCarpetOption);

            return carpetOption;
        }//end ReadCarpetOption


        /// calculates the cost of carpeting a room
        /// preconditions: true
        /// postconditions: displays how much it will cost to carpet a room (with cirtain dimensions) with a specific type of carpet
        static double CalculateCarpetCost(double lengthInMeters, double widthInMeters, int carpetSelection)
        {
            const double breber = 39.95;
            const double frieze = 34.95;
            const double pile = 23.59;
            const double plush = 19.95;
            double carpetCost;
            //  previousDimensions = DimensionsEntered(ref length,ref width);
            //  if (previousDimensions == false)
            //  {
            //     length = ReadLength();
            //      width = ReadWidth();
            //  }

            switch (carpetSelection)
            {
                case 1: carpetCost = breber * lengthInMeters * widthInMeters;
                    break;
                case 2: carpetCost = frieze * lengthInMeters * widthInMeters;
                    break;
                case 3: carpetCost = pile * lengthInMeters * widthInMeters;
                    break;
                case 4: carpetCost = plush * lengthInMeters * widthInMeters;
                    break;
                default: Main();
                    carpetCost = 0;
                    break;
            }

            Console.WriteLine("The cost of carpeting the room with {0} carpet is {1:c}", carpetSelection, carpetCost);

            return carpetCost;
        }//carpet calculatro
    }



}



C#

Posted
Comments
Pranay Rana 11-Apr-12 1:33am    
Dont forget to mark answer as accpeted if it work for you ....

its giving you error because you are declaring variable and than using that variable without assigning value to it that not allowed in c#

C#
 int menuOption;
double lengthInMeters=0;
double widthInMeters=0;
do
{
    MainMenu();
    menuOption = ReadOption();
    PerformMenuOption(menuOption, lengthInMeters, widthInMeters);


} while (menuOption != 4);


change your code as above resolve your issue...
 
Share this answer
 
Comments
credm5 11-Apr-12 21:03pm    
this does get rid of the errors, however when i exit from the carpet menu it reinitializes the lengthInMeteres and widthInMeteres back to zero. Which i don't want as it asks the user for the length and width again. I only want the user to enter these variables once and the program to use them for every option .I want the only way to change the length and width to be selection 3(enter/change dimension) in the mainMenu(). do u have any suggestions ?
You do not need to pass those two variables as parameters to PerformMenuOption. Change the signature of that function, and define the variables where you really need them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900