I'm still new to programming in high-level programming languages, so I don't know if this is a simple solution, but I'm still happy to learn. I have programmed a small emergency program in C #, which allows the user to enter an alarm within a few seconds, which should go out. It works fine, but the input the user must give must be a number. When the user enters any form of text, the program crashes. Now, how can I prevent users from entering text and calling a function, or doing something else when the user does this, and not just a program crash?
This is the code I have now:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Alarm { public static void play() { int sec; sec = Convert.ToInt16(Console.ReadLine()); for (int i = 0; i < seconds; ++i) { System.Threading.Thread.Sleep(1000); } for (int i = 0; i < 10; i++) { Console.Beep(); } } } }
source share