Subject: Beginners trouble Console
Posted By: sis Post Date: 1/8/2004 6:40:49 AM
Hi
I´m just starting to use /learn C # and my first question is how can I make the Console window stop.  That is it pops up and straight down again.  

And another beginners quest. how can I check the input from a console window. I mean I want a number - take it in as string and then convert to int... which ofcourse crashes my program if the user has written a letter... how can I check the input before I convert it to int?

Thanks in advance
Sis

Reply By: KABay Reply Date: 1/8/2004 10:20:11 AM
< 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;

        }
    }
}


Reply By: Imar Reply Date: 1/8/2004 12:20:06 PM
Another way to keep the console open is to put a ReadLine() statement at the end of your code. This will make the app wait for some user input, so if you then press Enter, the app will close.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

Go to topic 6887

Return to index page 973
Return to index page 972
Return to index page 971
Return to index page 970
Return to index page 969
Return to index page 968
Return to index page 967
Return to index page 966
Return to index page 965
Return to index page 964