Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my first day of coding.when I press F5 to see out put it just shows for a second .how do I see the output ?
below is my code
regards

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mod1_Lab1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create variable of different data types
            // initialize with a default value
            string firstName = "";
            string lastName = "";
            int age = 0;
            string street = "";
            string city = "";
            string country = "";
            DateTime birthDate;

            // Assign some value
            firstName = "Tom";
            lastName = "Thumb";
            age = 48;
            street = "street 1";
            city = "anytown";
            country = "US";
            birthDate = new DateTime(2013, 6, 5);

            // output to the console window

            // use simple output with just variable name
            Console.WriteLine(firstName);
            Console.WriteLine(lastName);

            // use palce holder style
            Console.WriteLine("{0} years old.", age);

            // use string concatenation
            Console.WriteLine(street + ", " + city + ", " + country);

            // use string interpolation
           // Console.WriteLine("Born on {birthDate}");

        }
    }
}
Posted
Updated 8-Apr-18 23:08pm
v2

You have a console application and run it from within Visual Studio. That will open a Windows command prompt window (a console) and execute your application. When your application terminates, the command prompt window is also closed.

To avoid closing that window and read what your application has printed, call a command that reads from the keyboard and blocks until something has been entered.

In your case calling the Console.ReadKey Method (System)[^] at the end of your Main function will do the job.
 
Share this answer
 
Comments
Member 13769540 9-Apr-18 5:08am    
added this and it works thanks

while (Console.ReadKey().Key != ConsoleKey.Enter) { }
Jochen Arndt 9-Apr-18 5:36am    
That is like the other solutions which all require Enter to be pressed.
When using Console.ReadKey(), pressing any key besides modifier keys like Shift, Ctrl, Alt will close the window.
Make the last line of your code something like

Console.ReadLine();


That will make the console pause until you press enter giving you an opportunity to see the output.
 
Share this answer
 
The problem is that it runs, reaches the end of the main method, and the app exits - so you can't see the output.

There are two ways to get round this, and they both involve "stopping" your app before it exits.
For a Console app like this, that's pretty easy: add one line just before teh end of teh method:
Console.WriteLine(street + ", " + city + ", " + country);

   // use string interpolation
   // Console.WriteLine("Born on {birthDate}");
   Console.ReadLine();   // <<<--- Add this.
}

The other is a little more complex, but a lot more flexible: use the debugger.
In Visual Studio, put the cursor on the last line of the method - the closing curly bracket. Look at the menu bar and open the "Debug" menu.
Select the "Toggle Breakpoint" option. You will see a "Red dot" appear at the beginning of the line - that says "Breakpoint here" and now you know where it is you can click that area and it will toggle a breakpoint at that line. "Red dot" there = breakpoint, "no Red Not" = no breakpoint.
When you have a breakpoint set, run your app using F5 again - this time, when it hits the breakpoint line, your app will stop and you can look at the output or - much more usefully - you can use the debugger to see your variable contents, and control your app while it is running! Get some practice with this, it's the best friend a developer can have!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900