|
-
June 29th, 2012, 09:15 AM
#1
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.
-
June 29th, 2012, 09:46 AM
#2
Re: My simple program closes automatically (New C# user)
Console.WriteLine("Press anykey to quit");
Console.ReadKey(true);
-
June 29th, 2012, 09:52 AM
#3
Re: My simple program closes automatically (New C# user)
 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?
-
June 29th, 2012, 10:06 AM
#4
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.
-
June 29th, 2012, 10:29 AM
#5
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.
-
June 29th, 2012, 11:43 AM
#6
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.

-
June 29th, 2012, 02:52 PM
#7
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 ?
-
June 29th, 2012, 04:15 PM
#8
Re: My simple program closes automatically (New C# user)
That's all the code there is.
-
July 1st, 2012, 12:59 AM
#9
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();
}
}
}
}
-
July 18th, 2012, 12:43 AM
#10
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??????
-
July 18th, 2012, 12:48 AM
#11
Re: My simple program closes automatically (New C# user)
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|