0

I've been trying to figure out how to get my console to close when someone finally types the wrong answer (in my quiz)?

I've tried Environment.Exit(); but it doesn't work... It gives me the error: "no overload for method 'Exit' takes 0 arguments.

Here's a bit of my code. I've marked with a comment where I'd want the quit code to be:

Console.WriteLine("In what country was Adolf Hitler born?");

string userAnswer = Console.ReadLine();

if (Answers.AnswerOne.Equals(userAnswer))
{
    Console.WriteLine("Congratulations! {0} is the correct answer!", Answers.AnswerOne);
    Console.WriteLine("\n\n(Press Enter to move on to the next question...)");
}
else if (!Answers.AnswerOne.Equals(userAnswer))
{
    Console.WriteLine("Sorry! You wrote the wrong answer!\nThe correct answer was {0}.", Answers.AnswerOne);
    Console.WriteLine("\n\n(Press Enter to exit...)");
    Console.ReadKey();
    //EXIT CONSOLE
}

Otherwise, the code runs perfectly fine.

Thanks in advance.

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54
user1779342
  • 49
  • 1
  • 1
  • 6
  • Do not use `Console.ReadKey();` nad try `Environment.Exit(0);` or `Environment.Exit(1);` – Sami Oct 28 '12 at 13:51

2 Answers2

1

The error message you are getting is 100% correct. Environment.Exit() needs to be called with a parameter.

Try Environment.Exit(0). The .Exit() method takes an int value to indicate the 'exit code'.

Exit code to be given to the operating system. Use 0 (zero) to indicate that the process completed successfully.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
Rob P.
  • 14,921
  • 14
  • 73
  • 109
1

I think you will find multiple answers here: How do I specify the exit code of a console application in .NET?

Google is your friend. :)

As for Environment.Exit(int exitCode):

// Summary:
//     Terminates this process and gives the underlying operating system the specified
//     exit code.
//
// Parameters:
//   exitCode:
//     Exit code to be given to the operating system.

0 is the default exit code when nothing went wrong.

Community
  • 1
  • 1
LightStriker
  • 19,738
  • 3
  • 23
  • 27