user1779342
user1779342

Reputation: 49

C# Console close when someone types the wrong answer

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.

Upvotes: 0

Views: 1389

Answers (2)

Rob P.
Rob P.

Reputation: 15081

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.

Upvotes: 1

LightStriker
LightStriker

Reputation: 21024

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.

Upvotes: 1

Related Questions