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)
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);
}
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);
}
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;
}
if (!ValidMenuOption)
{
Console.WriteLine("\n\t You have entered an invalid option please enter 1, 2, 3 or 4");
MainMenu();
}
} while (!ValidMenuOption);
return option;
}
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)
{
}
else if (option == 3)
{
}
else
{
DateTime date = DateTime.Now;
Console.WriteLine("\n Today's Date and Time: {0}\n", date);
}
}
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");
}
} while (lengthInMeters < 2.4 || lengthInMeters > 4.5);
return lengthInMeters;
}
static double ReadWidth()
{
double widthInMeters;
do
{
Console.Write("Please eneter the width of the room you wish to carpet in meters:");
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");
}
} while (widthInMeters < 2.4 || widthInMeters > 4.5);
return widthInMeters;
}
public static bool DimensionsEntered(double lengthInMeters, double widthInMeters)
{
bool haveDimensionsBeenEntered = false;
if (lengthInMeters > 0 && widthInMeters > 0)
{
haveDimensionsBeenEntered = true;
}
return haveDimensionsBeenEntered;
}
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);
}
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;
}
if (!ValidCarpetOption)
{
Console.WriteLine("\n\t Option must be 1, 2, 3, 4 or 5");
CarpetMenu();
}
} while (!ValidCarpetOption);
return carpetOption;
}
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;
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;
}
}
}