mercredi 30 octobre 2019

C# how to set a conditional statement to except user input?

I have a simple console App that converts pounds to kilograms and vice-versa. What I'm attempting to do is if the user enters lb then run the function to convert pounds to kilograms, else if the user enters kg, then run the function to convert kilograms to pounds.

During the setup part of the condition in main, I get an error "Use of unassigned local variable 'lb'

...The Code (snippets):

//method to convert KG to Lbs

public void ConvertKg()
        {
            Console.WriteLine("C# KG to LB program\n");

            Console.Write("Enter a number in KG: ");
            double kilograms = Convert.ToDouble(Console.ReadLine());

            double pounds = kilograms * 2.20462262185;
            Console.WriteLine(kilograms + " kilograms is " + pounds + " pounds");


        }

//method to convert Lbs to KG

 public void ConvertLb()
        {
            Console.WriteLine("C# LB to KG program\n");

            Console.Write("Enter a number  in lbs:");
            double pounds_userEntry = Convert.ToDouble(Console.ReadLine());

            double kilogram_userEntry = pounds_userEntry * 0.453592;
            Console.WriteLine(kilogram_userEntry + " kilograms is " + pounds_userEntry + " pounds");


        }

...main: string lb, kg; string userInput = "";

    Console.Write("Enter either lb or kg:");           

    if(userInput == lb) // where the error occurs
    {
        var k = new ConvertNumber();
        k.ConvertLb();
    }
    else
    {
        var l = new ConvertNumber();
        l.ConvertKg();

    }
    Console.ReadLine();

...the problem seems to be within the approach I'm using to set up the conditional statement to accept the user's input. ...could I get some help as to what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire