CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2012
    Posts
    3

    My simple program closes automatically (New C# user)

    Hi there,

    I have only just started using C# and can't understand why when I open this console program it automatically closes.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //User entered variables
                Console.WriteLine("Enter number A");
                int a = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter number B");
                int b = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter number C");
                int c = Convert.ToInt32(Console.ReadLine());
    
                //Calculation
                int d = a * b;
    
                //Result
                Console.WriteLine(a + "x" + b + "x" + c + "=" + d);
                Console.WriteLine("Press enter to quit");
                Console.ReadLine();
    
            }
        }
    }
    No errors were reported by Visual 2010 Express.

    Thanks.

  2. #2
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: My simple program closes automatically (New C# user)

    Console.WriteLine("Press anykey to quit");
    Console.ReadKey(true);

  3. #3
    Join Date
    Jun 2012
    Posts
    3

    Re: My simple program closes automatically (New C# user)

    Quote Originally Posted by Erendar View Post
    Console.WriteLine("Press anykey to quit");
    Console.ReadKey(true);
    Thanks but I've just tried that and it still closes on me. Any ideas?

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: My simple program closes automatically (New C# user)

    Do you get to see any output? What if you run it with the "Start Without Debugging" command, from the Debug menu (or Ctrl+F5)? When run from VS with Ctrl+F5, it should automatically display "Press any key to continue..." before it exists, so maybe you'll get a chance to see what's going on. If a runtime exception occurred, a console app will print the error message right there in the window - it is possible that an exception was thrown, but that the window closed before you got a chance to see it.

    You can also put a breakpoint (click on the grey area on the left side, a big red dot will appear) on the first line of code inside Main(), and then start debugging with F5; this way, you'll be able to step through the code line-by-line (F10, or Debug menu, Step Over), and follow the execution of your application (just make sure you have both VS and your app visible at the same time).

    If any of this works, if you see an exception or something, post here.

  5. #5
    Join Date
    Nov 2011
    Posts
    36

    Re: My simple program closes automatically (New C# user)

    Curious...
    Line 21 did you mean to put:
    Code:
    int d = a * b * c;
    That's not causing your problem but just would make sense for what you put in Console.WriteLine
    I'd also change line 26 to Console.ReadKey();

    Besides that, this should not be closing automatically.

  6. #6
    Join Date
    Mar 2001
    Posts
    2,529

    Re: My simple program closes automatically (New C# user)

    Program runs just fine. There is something else going on. What version of visual Studio do you have? (Which .net framework are you on?)
    Last edited by ahoodin; June 29th, 2012 at 11:46 AM.
    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: My simple program closes automatically (New C# user)

    He's on VS 2010 Express.

    Is this all code you have benewen? Isn't there any other subs or codes ?

  8. #8
    Join Date
    Jun 2012
    Posts
    3

    Re: My simple program closes automatically (New C# user)

    That's all the code there is.

  9. #9
    Join Date
    Jul 2012
    Posts
    2

    Re: My simple program closes automatically (New C# user)

    try this ..
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("Enter number A");
                    int a = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter number B");
                    int b = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter number C");
                    int c = Convert.ToInt32(Console.ReadLine());
                    try
                    {
                        //Calculation
                        //Result
                        int d = a * b;
                        Console.WriteLine(String.Format("{0}x{1}x{2}={3}", a, b, c, d));
                        Console.WriteLine("Press enter to quit");
                        Console.ReadLine();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        Console.ReadLine();
                    }
    
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                
                }
            }
        }
    }

  10. #10
    Join Date
    Jul 2012
    Posts
    1

    Re: My simple program closes automatically (New C# user)

    Hi..
    I tried your code.. Its working properly... its not cloasing until i click enter..
    May i Know what's ur exact problem??????

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: My simple program closes automatically (New C# user)

    Quote Originally Posted by HTCc View Post
    Hi..
    I tried your code.. Its working properly... its not cloasing until i click enter..
    May i Know what's ur exact problem??????
    That is because
    Code:
    Console.ReadLine();
    Waits for user input, ie. Enter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured