Search code examples
c#loopsconsolereset

Program loses functionality after resetting


recently I have bumped into a problem with my C# code, at first my goal was to make a simple program to show keyboard key codes when the user presses a key on their keyboard, but it was only limited to one operation before the user had to exit and re-open the program again to do another operation.

So I decided to edit it and make it do more than one operation at one set (and possibly forever if not closed?)

First version of the code:

{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pressKey = "Write any character/symbol to find it's key code: \n\n";
            string asciiArt = "null";\\left it because it looks ugly here
            string quit = "Press 'Home' to exit and try again...";
            Console.Title = "ValueFinder";
            Console.Write(pressKey);
            Console.WriteLine(asciiArt);
            int Value = Console.Read();
            Console.WriteLine($"Keyboard Key Code Value:{Value}\n");
            Console.WriteLine(quit);
            while (true)
            {
                if (Console.ReadKey(true).Key == ConsoleKey.Home)
                {
                    break;
                }
            }
        }
    }
}

This one only does one operation,

So I edited the code to this:

static void Main(string[] args)
{
    string pressKey = "Write any character/symbol to find it's key code: \n\n";
    string asciiArt = "null";
    string quit = "Press 'Home' to try again...";
    Console.Title = "ValueFinder";
    Console.Write(pressKey);
    Console.WriteLine(asciiArt);
    int Value = Console.Read();
    Console.WriteLine($"Keyboard Key Code Value:{Value}\n");
    Console.WriteLine(quit);

    while (true)
    {
        if (Console.ReadKey(true).Key == ConsoleKey.Home)
        {

            Console.Clear();
            Console.Write(pressKey);
            Console.WriteLine(asciiArt);
            int Value1 = Console.Read();
            Console.WriteLine($"Keyboard Key Code Value: {Value}\n");
            Console.WriteLine(quit);
            while (true)
            {
                if (Console.ReadKey(true).Key == ConsoleKey.Home)
                    break;
            }
        }
    }
}

The problem here, the program completely loses functionality after pressing 'Home' key which is supposed to reset the console to the default screen when you first open the program, it actually does but with totally no functionality, I figured out that after pressing the 'Home key' for about 5 times or more it will regain functionality but after doing one operation the same things happen over and over.

second problem which I do not know if it affects something, int Value1 = Console.Read(); , can't be named 'Value' as it will give me an error:

Error CS0136 A local or parameter named 'Value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter.

Third problem, I'm totally unexperienced and just started a couple days ago so I can't seem to figure out this problem that may be happening because I'm just doing it the wrong way, so please be patient with me.

Renamed local variable 'Value' to 'Value1' and it fixed the error, but now it's "an unnecessary assignment value to value1"


Solution

  • Value = Console.Read(); Seemed to fix the issue of functionality as user @ralf mentioned.

    As for the first issue it was because the console was printing "Enter" ASCII code (10) and Carriage return (13).