Click to See Complete Forum and Search --> : [RESOLVED] Console.ReadLine issues
edkilp
April 10th, 2008, 09:18 PM
Hi all. I'm working through Jesse Liberty's book Programming C# 3.0 5th edition. I'm using VS2008, and every time I try to run one of these simple console applications, the darn thing shows up for a split second and disappears. I know that I need to add Console.ReadLine, but my question is this: why isn't it ever included in the original example code? Is there some secret trick to make the window stay put automatically, or is this something that happens in VS2008, but not in 2005? It's really discouraging me, because I tend to type everything as opposed to copy and paste,and then I have to add that line after every single WriteLine. Am I missing something here? Thanks!
Ed
dglienna
April 10th, 2008, 09:26 PM
Sure. They call it a console application because it runs in a console window. To simulate that in windows, just type CMD for a command prompt. Then it will run as expected.
edkilp
April 11th, 2008, 05:42 AM
Hi there. I understand what a console application is. My question is about the examples in almost every book I've looked at. If I'm brand-new at this, which I pretty much am, and I'm working through a book's examples, and they don't work, even using copy and paste, that leads to discouragement. I know that Console.ReadLine() makes my problem go away, and I want to know if there's something I need to add so it will be there without having to type it after every Console.WriteLine(). Try this code in VS2008 and see if it runs for you:
using System;
class Values
{
static void Main( )
{
int valueOne = 10;
int valueTwo = 20;
if ( valueOne > valueTwo )
{
Console.WriteLine(
"ValueOne: {0} larger than ValueTwo: {1}",
valueOne, valueTwo);
}
else
{
Console.WriteLine(
"ValueTwo: {0} larger than ValueOne: {1}",
valueTwo,valueOne);
}
valueOne = 30; // set valueOne higher
if ( valueOne > valueTwo )
{
valueTwo = valueOne + 1;
Console.WriteLine("\nSetting valueTwo to valueOne value, ");
Console.WriteLine("and incrementing ValueOne.\n");
Console.WriteLine("ValueOne: {0} ValueTwo: {1}",
valueOne, valueTwo);
}
else
{
valueOne = valueTwo;
Console.WriteLine("Setting them equal. ");
Console.WriteLine("ValueOne: {0} ValueTwo: {1}",
valueOne, valueTwo);
}
}
}
I don't yet know how you guys add those fancy code snippets in this forum, but I copied and pasted this directly from the book. It compiles with no errors, and runs, but again, it only stays on my screen for a split second. If I add Console.ReadLine() after every instance of Console.WriteLine, it's fine, but that makes me wonder why the readline isn't included in the example code. Especially if the author knows that this is going to be impossible to read.
This book uses VS2008, so I'm using the same thing. I have to be missing something. Typing cmd and running the exe gives me the same result. It pops up for a second, then goes away. I'm getting irritated!! Anyway, I'll keep trying, and thanks for the reply.
ed
edkilp
April 11th, 2008, 05:49 AM
OK, I'm mistaken about the command line thing. I can open a command prompt, navigate to the directory containing my exe and run it. All well and good, but shouldn't I be able to see the result in VS2008 without having to go through all that? I can't imagine that I have to do all this every time I wanna test a new chunk of code. Please help me before I throw in the towel!! Thanks
ed
nelo
April 11th, 2008, 05:54 AM
It compiles with no errors, and runs, but again, it only stays on my screen for a split second.
How are you running it? I guess is that you are running it from Visual Studio.
I'm getting irritated!!
No need for that. You need to understand the nature of the application. It is a console application. Once you reach the end for the main function the application terminates. If you want to see something you have one of two options:
Option1: launch a command prompt window yourself. browse to the directory where your executable is located. run the executable from the command prompt.
e.g. c:\>myApp.exe
Option2: set up the cmd.exe (located in your Windows\System32 folder if you're using XP or earlier) as the start up program in your Debug settings. Perhaps that will do the trick.
You actually have a third option which is to put a break point at the end of the main function.
edkilp
April 11th, 2008, 06:12 AM
You know what I'm doing? I'm debugging the thing,and that's causing my headache. I need to select start without debugging. Then it runs fine. Another hurdle overcome! I'm almost a certified expert programmer now!! Thanks for being patient! I'll mark this resolved
ed
Mutant_Fruit
April 11th, 2008, 09:15 AM
Put a single Console.ReadLine () at the end of the Main method. This will prevent the application from exiting until you hit enter. There's no need to add one after each Console.WriteLine unless you *need* it to pause after each writeline.
Thing is, you'd have this exact issue no matter what language you are coding in. This isn't a C# issue, it's just how console applications run in windows.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.