Question

Using MS Visual Studio 2010 and the program is based in C# Console Application.

I have a while loop with an sInput = Console.ReadLine(); and then a switch inside.

When I run my code the sInput is skipped in the first occurrence of the while loop (Which automatically gives the default value of the switch).

But it works properly from the second occurrence of the While loop.

Why does it skip sInput in the first occurrence?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Imaging;

namespace game_Console
{
    class Program
    {
        [DllImport("kernel32.dll", ExactSpelling = true)]

    private static extern IntPtr GetConsoleWindow();
    private static IntPtr ThisConsole = GetConsoleWindow();

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    private const int HIDE = 0;
    private const int MAXIMIZE = 3;
    private const int MINIMIZE = 6;
    private const int RESTORE = 9;

    static void Main(string[] args)
    {
        Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
        ShowWindow(ThisConsole, MAXIMIZE);

        string sMenu, sIntro, sInput;
        bool bExit, bCredits;

        sMenu = "********************\n Pokemon Console\n********************\n\n Type in the desired number(1-5) and press Enter.\n--------------------\n 1. New Game\n 2. How To Play\n 3. High Scores\n 4. Credits\n 5. Exit\n____________________\n";
        sIntro = "\n        ########  ########  ##    ##  ########  ##    ##  ########  ##    ##        ########  ########  ##    ##  ########  ########  ##        ########\n        ########  ########  ##   ##   ########  ###  ###  ########  ###   ##        ########  ########  ###   ##  ########  ########  ##        ########\n        ##    ##  ##    ##  ##  ##    ##        ## ## ##  ##    ##  ###   ##        ##    ##  ##    ##  ###   ##  ##        ##    ##  ##        ##\n        ##    ##  ##    ##  ## ##     ##        ## ## ##  ##    ##  ## #  ##        ##        ##    ##  ## #  ##  ##        ##    ##  ##        ##\n        ########  ##    ##  ###       ####      ##    ##  ##    ##  ## #  ##        ##        ##    ##  ## #  ##  ########  ##    ##  ##        ####\n        ########  ##    ##  ###       ####      ##    ##  ##    ##  ##  # ##        ##        ##    ##  ##  # ##  ########  ##    ##  ##        ####\n        ##        ##    ##  ## ##     ##        ##    ##  ##    ##  ##  # ##        ##        ##    ##  ##  # ##        ##  ##    ##  ##        ##\n        ##        ##    ##  ##  ##    ##        ##    ##  ##    ##  ##   ###        ##    ##  ##    ##  ##   ###        ##  ##    ##  ##        ##\n        ##        ########  ##   ##   ########  ##    ##  ########  ##   ###        ########  ########  ##   ###  ########  ########  ########  ########\n        ##        ########  ##    ##  ########  ##    ##  ########  ##    ##        ########  ########  ##    ##  ########  ########  ########  ########\n ______________________________________________________________________________________________________________________________________________________________\n                                                                      Press Enter to continue";
        bExit = false;
        bCredits = false;

        Image Picture = Image.FromFile(@"C:\Users\Home\Desktop\pokemon console\Pokemon Console\Pokemon Console\Data\pokemon\pics\00.png");
        Console.SetBufferSize((Picture.Width * 0x2), (Picture.Height * 0x2));
        FrameDimension Dimension = new FrameDimension(Picture.FrameDimensionsList[0x0]);
        int FrameCount = Picture.GetFrameCount(Dimension);
        int Left = Console.WindowLeft, Top = Console.WindowTop;
        char[] Chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };
        Picture.SelectActiveFrame(Dimension, 0x0);
        for (int i = 0x0; i < Picture.Height; i++)
        {
            for (int x = 0x0; x < Picture.Width; x++)
            {
                Color Color = ((Bitmap)Picture).GetPixel(x, i);
                int Gray = (Color.R + Color.G + Color.B) / 0x3;
                int Index = (Gray * (Chars.Length - 0x1)) / 0xFF;
                Console.Write(Chars[Index]);
            }
            Console.Write('\n');
        }


        Console.WriteLine(sIntro);
        Console.Read();
        Console.Clear();

        while (bExit == false)
        {
            Console.Clear();
            Console.WriteLine(sMenu);
            sInput = Console.ReadLine();//Skips this line in the first occurrence

            switch (sInput)
            {
                case "1":
                {
                    Console.WriteLine("one");
                    Console.ReadLine();
                    break;
                }
                case "2":
                {
                    Console.WriteLine("two");
                    Console.ReadLine();
                    break;
                }
                case "3":
                {
                    Console.WriteLine("three");
                    Console.ReadLine();
                    break;
                }
                case "4":
                {
                    Console.WriteLine("four");
                    Console.ReadLine();
                    break;
                }
                case "5":
                {
                    bExit = true;
                    break;
                }
                default:
                {
                    Console.WriteLine("error");
                    Console.ReadLine();
                    break;
                }                    
            }

        }

        Console.Clear();
        if (bCredits == false)
        {                
            Console.WriteLine(" Take a look at our Credits before you leave");
            TextReader tr = new StreamReader("Credits.txt");
            Console.WriteLine(tr.ReadToEnd());
            tr.Close();
        }
        Console.WriteLine("\n +++++++++++++++++++++++++\n ++Thank you for playing++\n +++++++++++++++++++++++++\n\n    Press Enter to Exit");
        Console.Read();
        Console.Read();
    }
}
}

EDIT:Added all my code to establish if problem originated from previous code outside the while loop.

Was it helpful?

Solution

My guess is that input buffer is not empty and contains newline. Could it be that your provided code is taken out of context where some other data input occurs?

Try to add following code before skipped line:

while (Console.KeyAvailable)
    Console.ReadKey(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top