< How can I make the Console window stop. That is it pops up and straight down again. >
I assume you are running your app within the VS Development Environment having chosen Debug / Start.
If you have set no breakpoints and your app runs to completion without any errors the console window it has been running in will close as the app ends. Otherwise you will get an error message in the environment (Debugging is active).
You can set a breakpoint at the last executable line of your app (in Main) and then the execution will stop and the window will still be open (for you to examine results sent to screen?).
Or you can chose Debug / Start without debugging. When the app ends you will be prompted to 'Press any key to continiue' in the console window when your app ends.
Or, having compiled your app to an executable, open a VS Command Prompt [ START / Programs / Microsft Visual Studio .Net / Visual Studio .Net Tools / Visual Studio .Net Command Prompt ], navigate to the folder your executable resides in and run it from there. The window will stay open until you close it.
< How can I check the input from a console window >
Using Console.ReadLine get the user's input and then test it to make sure it is numeric.
using System;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
String str;
int i;
i = -1;
Console.WriteLine("Enter a number, -1 to end");
while (i == -1)
{
// Get the user's input, converting it to a string
str = Console.ReadLine().ToString();
try
{
// Try converting the string back to an integer
// If there is a problem an error will be thrown
i = Int16.Parse(str);
}
catch
{
// and the error will be caught and handled here
// Let the user know they entered a bad 'number'
// and should try again.
Console.WriteLine("Invalid numeric entry");
// reset i to be sure we will stay in the loop
// in order to try again
i = -1;
}
}
Console.WriteLine ("Echo: {0}", i.ToString());
Console.WriteLine ("Done");
return;
}
}
}
|