Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 8th, 2004, 07:40 AM
sis sis is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sis
Default Beginners trouble Console

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

 
Old January 8th, 2004, 11:20 AM
Authorized User
 
Join Date: Nov 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to KABay
Default

< 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;

        }
    }
}
 
Old January 8th, 2004, 01:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ projects for beginners 132591 C++ Programming 5 September 2nd, 2011 02:41 AM
Console application but no Console gamotter C# 2 August 22nd, 2007 11:41 PM
A better book for beginners alexmbr BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 2 August 10th, 2007 04:52 AM
Beginners Error! aadz5 Apache Tomcat 2 February 13th, 2006 05:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.