Answered Escape Key

DixreuX

New member
Joined
Sep 24, 2020
Messages
2
Programming Experience
Beginner
I have an assignment where i have to create a calculator, and i need to be able to close the cmd.exe by pressing the ESC key. The calculator is working as intended and it restarts because of my while loop. My
problem is that i cant seem to find a method that makes the "quit at any time by using the ESC key" function work (which is why theres no escape key code below)

7+ hours of troubleshooting and googling and im no closer to implementing the ESC function.

Am i doing something wrong?
Does anyone have a possible solution that works?

Ive tried all the solutions and methods i could possibly find and still nothing works.
The full code below is my program and my while loop and the reason i am posting all of it is because i am not sure where in the code i should implement the escape key function (could not find a single answer to these questions, which is why i am desperate.)

C#:
static void Main(string[] args)
{
    while (true) {
        Console.WriteLine("Tryk ESC for at stoppe ellers tryk ENTER");
        Console.WriteLine("Velkommen til ConCalc");
        Console.WriteLine("---------------------");
        Console.WriteLine("1. Plus");
        Console.WriteLine("2. Træk fra");
        Console.WriteLine("3. Gange");
        Console.WriteLine("4. Dividere");
        Console.WriteLine("5. Nettoløn");
        Console.WriteLine("6. Kørselsfradrag");
        Console.WriteLine("---------------------");
        Console.Write("Vælg funktion: ");
        string funktion = Console.ReadLine();
        double resultat = 0;

        if (funktion == "1") {
            Console.Write("Tal 1: ");
            string tal1 = Console.ReadLine();
            Console.Write("Tal 2: ");
            string tal2 = Console.ReadLine();
            resultat = Convert.ToDouble(tal1) + Convert.ToDouble(tal2);
        }
        else if (funktion == "2") {
            Console.Write("Tal 1: ");
            string tal1 = Console.ReadLine();
            Console.Write("Tal 2: ");
            string tal2 = Console.ReadLine();
            resultat = Convert.ToDouble(tal1) - Convert.ToDouble(tal2);
        }
        else if (funktion == "3") {
            Console.Write("Tal 1: ");
            string tal1 = Console.ReadLine();
            Console.Write("Tal 2: ");
            string tal2 = Console.ReadLine();
            resultat = Convert.ToDouble(tal1) * Convert.ToDouble(tal2);
        }
        else if (funktion == "4") {
            Console.Write("Tal 1: ");
            string tal1 = Console.ReadLine();
            Console.Write("Tal 2: ");
            string tal2 = Console.ReadLine();
            resultat = Convert.ToDouble(tal1) / Convert.ToDouble(tal2);
        }
        else if (funktion == "5") {
            int hun = 100;
        
            Console.Write("Indtast din bruttoløn ");
            String bruttoløn = Console.ReadLine();
            Console.Write("Indtast din trækprocent ");
            String trækprocent = Console.ReadLine();
            Console.Write("Indtast dit månedsfradrag ");
            String månedsfradrag = Console.ReadLine();
            resultat = Convert.ToDouble(bruttoløn) - Convert.ToDouble(månedsfradrag) * Convert.ToDouble(trækprocent) / Convert.ToDouble(hun);
        }
        else if (funktion == "6") {
            int km = 24;
            double kmp = 1.99;
            string kr;

            Console.Write("Indtast dine arbejdsdage pr. år ");
            String arbejdsdage = Console.ReadLine();
            Console.Write("Indtast antal kilometer til arbejde tur/retur ");
            String kilometer = Console.ReadLine();
            resultat = (Convert.ToDouble(kilometer) - Convert.ToDouble(km)) * Convert.ToDouble(arbejdsdage) * kmp;
        }
    
        Console.WriteLine("---------------------");
        Console.Write("Resultat: ");
        Console.WriteLine(resultat);
        Console.WriteLine("---------------------");
    }
}
 
Last edited by a moderator:
You need to do some work on your posting. You don't ask questions in the title and post nothing but code. The post should contain a FULL and CLEAR explanation of the problem, i.e. what you're trying to achieve, how you're trying to achieve it and what happens when you try. The title should be written last and be a concise summary of the topic of the post. Also, we don't need to see all the code you have. What we need is the simplest code that demonstrates the issue.
 
You won't be able to detect it with Console.ReadLine, you have to use Console.ReadKey.
 
Back
Top Bottom