-1

I'm creating a Desktop application (console) for Windows, using C#.

This is my code:

namespace myapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
            Console.WriteLine("Type 'exit' to exit!");
            string line = Console.ReadLine();
            if (line == "exit") {
                Environment.Exit(0);
            }
            if (line == "copyright") {
                Console.WriteLine("Copyright 2017 TIVJ-dev");
            }
        }
    }
}

If I type "exit", it works fine (I'm sure it does this Environment.Exit(0); action). But if I type "copyright", it does not work. I can see an empty line instead. I started with C# today, so my apologies if this is very beginner problem. I haven't found solution on the internet.

Screenshot:

Copyright command is not doing anything

Kuvaaja
  • 51
  • 12

4 Answers4

3

I'm not sure in what way it doesn't work. It should run

Console.WriteLine("Copyright 2017 TIVJ-dev");

then immediately close the console window. Try putting another

Console.ReadLine();

at the end of Main().

spodger
  • 1,668
  • 1
  • 12
  • 16
1

You could use an else if statement;

   namespace myapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
            Console.WriteLine("Type 'exit' to exit!");
            string line = Console.ReadLine();
            if (line == "exit")
            {
                Environment.Exit(0);
            }
            else if (line == "copyright") {
                Console.WriteLine("Copyright 2017 TIVJ-dev");
            }
        }
    }
}

Edit

By using an if/else this does the same as using multiple if statements however this is a more efficient way and make it easier when using break points.

Tom
  • 12,928
  • 2
  • 15
  • 31
  • 1
    What difference does this make at all to the original code? The original code **worked**. – Matt Hogan-Jones Sep 06 '17 at 10:26
  • Can people please advise why this answer is being down voted? else if is a vaild way of checking? – Tom Sep 06 '17 at 10:29
  • 1
    @Tom not the downvoter, but I assume it was because the original code is fine and should work, the OP had/has another issue completely. Your addition doesn't change anything for this example – FakeCaleb Sep 06 '17 at 10:32
  • 1
    @Tom if/else probably more efficient, but aside from that it does the same thing as OP's original code. Can't see how it would solve the problem – Riv Sep 06 '17 at 10:33
  • 1
    @Kuvaaja I don't know how many more times I can say this, but the problem is not in your code. Your original code worked. Considering that the first if statement would **terminate the process** I don't see how an `else if` can be considered to be any better. – Matt Hogan-Jones Sep 06 '17 at 10:34
0

Try launching the application by pressing ctrl F5 in visual studio and seeing if it works. Alternatively click on debug->start without debugging.

Then write "copyright" and press enter.

What is probably happening is that the console is printing the line, then closing before you have a chance to see it. When you don't use the debugger the console stays open even once the application is finished.

Yair Halberstadt
  • 5,733
  • 28
  • 60
-1

Now everyone: Now this project works fine! Thanks for people who said add another Console.ReadLine. When I tested else if, I forgot there was that also. But now it works, so I try to close this. Thanks everyone!

Kuvaaja
  • 51
  • 12