Simple App C# console in Visual Studio 2017, closes without showing the result

Asked

Viewed 2,152 times

0

I’m trying to run a simple program that writes "Hello world" on VS2017:

namespace OlaMundo
{
    class Program
    {
        static void Main()
        {
            int num;

            System.Console.WriteLine("Número :");

         num = System.Console.Read();

            System.Console.WriteLine(num);
        System.Console.WriteLine("Tecle enter para fechar...");

        System.Console.ReadLine();

    }
}
}

but in executing it I come across the following problem:

"OlaMundo.exe" (CLR v4.0.30319: DefaultDomain): Carregado "C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll". Não é possível localizar ou abrir o arquivo PDB.
"OlaMundo.exe" (CLR v4.0.30319: DefaultDomain): Carregado "C:\Users\tiago\source\repos\C#\01_Hello_world\OlaMundo\OlaMundo\bin\Debug\OlaMundo.exe". Símbolos carregados.
O programa "[1688] OlaMundo.exe" foi fechado com o código 0 (0x0).

I’ve disabled the option "Just my code" and I’ve also deleted the folder "bin" of the project and recompiled again. However, I did not have any success.

  • @Tiagosantos, you got it solved?

1 answer

1

Apparently there is nothing wrong, the console closes because the application has finished its flow... if you want to keep the window open and keep reading the "Hello, Wolrd", add a waiting point, as the input of some value.

Your second problem is because you are not converting the type and using the wrong method. All inputs via console are String type, in your code you need to validate and convert to the expected type.

namespace OlaMundo
{
    class Program
    {
        static void Main(string[] args)
        {

            int num;
            Console.WriteLine("Número :");
            num = Convert.ToInt32(System.Console.ReadLine());

            Console.WriteLine(num);

            //Adicione esse trecho ao final da sua Main
            #if DEBUG
            Console.WriteLine("Tecle enter para fechar...");
            Console.ReadLine();
            #endif
        }
    }
}
  • "Resolved, "however, when I declare variable in the same program, inserted the data through the console, it closes before showing the result of what I entered on the screen. In addition to returning the same warning on exit.

  • It’s the same thing, always leaves one Console.Read() at the end so you can keep the console open while debugging. And it’s not "solved", this is the solution.

  • When I declare type variable int in the same program, entering the data through the console, it closes before showing the result of what I entered on the screen. In addition to returning the same warning on exit.

  • Look at the editing of my answer. And add the rest of your code to the question, the problem probably is, but it’s turning into another question :P.

  • Well, it was precisely the solution of the edited answer that I applied, which sent the same output return, closing the console when I use variables of type int, which does not happen with strings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.