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.
Re: My simple program closes automatically (New C# user)
Console.WriteLine("Press anykey to quit");
Console.ReadKey(true);
Re: My simple program closes automatically (New C# user)
Quote:
Originally Posted by
Erendar
Console.WriteLine("Press anykey to quit");
Console.ReadKey(true);
Thanks but I've just tried that and it still closes on me. Any ideas?
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.
Re: My simple program closes automatically (New C# user)
Curious...
Line 21 did you mean to put:
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.
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?)
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 ?
Re: My simple program closes automatically (New C# user)
That's all the code there is.
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();
}
}
}
}
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??????
Re: My simple program closes automatically (New C# user)
Quote:
Originally Posted by
HTCc
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