0
double UserInput;
string y, z;
double OunceToGram, PoundToOunce, PoundToKilogram, PintToLitre, InchToCenti, MilesToInch;


OunceToGram = UserInput * 28.0; //(error is here, it cannot find UserInput)
PoundToOunce = UserInput * 16.0;
PoundToKilogram = UserInput * 0.454;
PintToLitre = UserInput * 0.568;
InchToCenti = UserInput * 2.5;
MilesToInch = UserInput * 63360.0;

int i = 0;

while (i < UserInput)
{
    Console.WriteLine("");
    Console.WriteLine("Please enter a unit to convert, type a num <1 to cancel");
    UserInput = Convert.ToDouble(Console.ReadLine());
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Can you please show the code where UserInput is assigned to? – Aaron Apr 10 '16 at 17:32
  • 1
    Are you sure that this is the error message? Instead, from the code above, I think you should have `use of unassigned local variable....` or something like that – Steve Apr 10 '16 at 17:33
  • Initialize userinput to something before trying to use it – nhouser9 Apr 10 '16 at 17:39
  • I can`t initialise it to a number because then the program will just accept the input as that number. Steve yes the error is unassigned local variable – Ross Seward Apr 10 '16 at 17:59
  • Then you have no choice but you need to initialize it to something. You cannot use a variable before initializing it. It will be helpful if you add more of your code that leads to this calcs and explain better your constraints. What do you mean with _the program will just accept the input as that number_? – Steve Apr 10 '16 at 18:02
  • Basically the program will ask for a input from the user, then it will take that number and depending on what the user chooses to convert from and to the programme will then look up say, ounce to gram and then do that conversion, so if UserInput = 5 and then the user wants Ounces to Grams it will then do 5 * 28.0. But if the variable is initilialised to 1 for example it will ignore the users input and just do 1 * 28.0 every time. I apologise for lack of code. – Ross Seward Apr 10 '16 at 18:11

1 Answers1

2

You could resolve your problem not converting immediately the user input to your variable UserInput and checking if the user types a conventional letter to stop the input

double UserInput = 0.0;  // <- You need to initialize before using it ...
.....

string stopInput = "N";
while (stopInput != "Q"))
{
   Console.WriteLine("");
   Console.WriteLine("Please enter a unit to convert, type 'Q' to cancel");
   stopInput = Console.ReadLine();
   if(stopInput == "Q")
      break;
   UserInput = Convert.ToDouble(stopInput);
   ....
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Steve this works and I used <1 to cancel instead of a letter, but the problem remains that if I make UserInput initialised to 0.0 then it will take that into the equation instead of what the user inputs. – Ross Seward Apr 10 '16 at 18:50