Skip to content

Instantly share code, notes, and snippets.

@NeonixRIT
Created October 30, 2019 23:02
Show Gist options
  • Save NeonixRIT/0230bee0f9f6407d73fa233737d44c9f to your computer and use it in GitHub Desktop.
Save NeonixRIT/0230bee0f9f6407d73fa233737d44c9f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoundsToKilograms
{
class Program
{
static void Main(string[] args)
{
string[] pounds = { "pounds", "lbs", "pound", "lb", "p" };
string[] kilograms = { "kilograms", "kg", "kilos", "kilo", "k" };
bool validResponse = true;
bool goAgane = true;
//Loop to be able to repeatedly input answers to not have to reopen if there are multiple things to convert
do
{
Console.WriteLine("Please input what you want to convert. Please include the unit after a space.");
Console.WriteLine("Type exit to close the application");
//Loop to retake an input if something invalid is entered
do
{
//Needs to be reset to true every loop
validResponse = true;
//splits users input into and array (separator is a space) that should be [int weight, string unit]
string[] input = Console.ReadLine().Split(new char[0]);
//If they want to close the application
if (input[0] == "exit")
{
goAgane = false;
}
//If they dont input at least 2 items into the string, then an error is created when trying to set the unitInput variable
//Sets validResponse to false so it loops and asks again for a proper response
else if (input.Length != 2)
{
validResponse = false;
Console.WriteLine("Please input a number, space, and then a unit.");
}
else
{
string unitInput = input[1];
double weight = Convert.ToDouble(input[0]);
//Converts pounds to kilograms
if (pounds.Any(unitInput.ToLower().Contains))
{
Console.WriteLine();
Console.WriteLine($"{weight} pounds is {Math.Round((weight / 2.205), 2)} kilograms");
Console.WriteLine();
Console.WriteLine("Press enter to continue");
Console.ReadLine();
}
//Converts kilograms to pounds
else if (kilograms.Any(unitInput.ToLower().Contains))
{
Console.WriteLine();
Console.WriteLine($"{weight} kilograms is {Math.Round((weight * 2.205), 2)} pounds");
Console.WriteLine();
Console.WriteLine("Press enter to continue");
Console.ReadLine();
}
//If they don't input a proper unit that matches one of the arrays, let the user retry the input
else
{
validResponse = false;
Console.WriteLine("Please input a number, space, and then a unit");
}
}
}
while (!validResponse);
}
while (goAgane);
Environment.Exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment