G+_Tnt Seeker Posted March 5, 2014 Share Posted March 5, 2014 Hey guys, really like the show, was testing Lou's code and added some sanitizers to check for input that's <= 0. here's the code using System; namespace TwiT.Tv.Coding101 { /*START copying from here to paste in your program */ partial class Program { /***********Download the IDE***********/ //For Mac Users: http://monodevelop.com/Download //For Windows Users: http://www.visualstudio.com/downloads/download-visual-studio-vs /// /// Main function used in many console application. /// static void Main() { while (true) //Loop used to keep asking for user input. { Console.Write("Minutes until your appointment: "); decimal minutesLeft = decimal.Parse(Console.ReadLine()); //Gets mins as a decimal from the user. (Doesn't check if they dont enter a number) if (minutesLeft <= 0) //look's to see if it's too small. { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" {0} Is to little", minutesLeft); Console.WriteLine("Press Enter"); Console.ResetColor(); Console.ReadLine(); Console.Clear(); Main(); } Console.Write("Distance you have to travel (in miles): "); decimal distanceInMiles = decimal.Parse(Console.ReadLine()); //Gets miles as a decimal from the user. (Doesn't check if they dont enter a number) if (distanceInMiles <= 0) //look's to see if it's too small. { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(" {0} Is to little", distanceInMiles); Console.WriteLine("Press Enter"); Console.ResetColor(); Console.ReadLine(); Console.Clear(); Main(); } Console.WriteLine(); int speedInMPH = RequiredSpeedInMPH(distanceInMiles, minutesLeft); //Calculate Required Speed using a function call //Write the text out with a space in between using {composite formatting} which is the {0} place holder that is replaced with the text. //http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx Console.WriteLine("Drive approximately {0} mph nonstop to be on time. Good Luck!", speedInMPH); //Display the result to the user int kilometerPerHour = ConvertMPHtoKPH(speedInMPH); Console.WriteLine("That is {0} km/h for those of you using the metric system.", kilometerPerHour); Console.ForegroundColor = ConsoleColor.Yellow; //Set the text color to yellow Console.WriteLine("**Please Follow Posted Speed Limits**"); //Write the warning to the user Console.ResetColor(); //Reset back to the original color Console.Write(" to continue, or q to quit: "); //Tell the user to use or q to quit. var key = (int)Console.ReadKey(true).Key; //Retrieve the user input if (key == 27 /* key*/ || key == 81 /* letter q*/) break; Console.Clear(); //Clear the console for the next try. } } /// /// Convert Miles Per Hour to Kilometers Per Hour /// /// Miles per hour as a whole number /// Returns Kilometers per hour as a whole number static int ConvertMPHtoKPH(int speedInMPH) { return (int)Math.Round(speedInMPH * 1.609344, 0); } /// /// Calculates Speed based on distance in miles and minutes until your appointment /// /// Distance as a decimale /// Minutes as a decimal /// Speed in MPH as a whole number static int RequiredSpeedInMPH(decimal distanceInMiles, decimal minutesLeft) { int requiredSpeed = 0; //initialize a variable for the calculated speed to return/ decimal amountOfHours = minutesLeft / 60; // 60 mins in an hour requiredSpeed = (int)Math.Round(distanceInMiles / amountOfHours, 0); //Use the Math library to round the decimal to the nearest whole number return requiredSpeed; //return the speed back to the function caller. } /// /// This is a method with no return value /// static void PrintMenu() { Console.Title = "TwiT.Tv Coding 101"; //Set the title of the console window. Console.WriteLine("*** Welcome to Coding 101! *"); //Print a welcome message Console.WriteLine(); Console.WriteLine("a Repeat a word."); Console.WriteLine("b Convert numbers to binary."); Console.WriteLine("c Say Hello!"); Console.WriteLine(); Console.Write("Select a menu item ( or q to quit):"); } Thanks for the show! http://www.twit.tv Link to comment Share on other sites More sharing options...
G+_Ben Parks Posted March 6, 2014 Share Posted March 6, 2014 Looks great! One tip though, there's a lot of repeating code that would be much easier to maintain if it we're put into functions! :) Link to comment Share on other sites More sharing options...
G+_Shannon Morse Posted March 6, 2014 Share Posted March 6, 2014 Nice job!! Link to comment Share on other sites More sharing options...
Recommended Posts