HelpConsole.Read

Posts 14 of 4 · Page 1 of 1
Console.Read
So I wrote this code:
Code:
class Program
    {
        static void Main(string[] args)
        {
            char l;
            Console.WriteLine("Press 1, 2, 3, 4, or 5. OR you can press 'f' for something special!");
            l = (char)Console.Read();
            switch (l)
            {
                case '1':
                    {
                        int q, a = '7';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        q = (int)Console.Read();
                        if (q == a)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }

                case '2':
                    {
                        int w, aw = '4';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        w = (int)Console.Read();
                        if (w == aw)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '3':
                    {
                        int e, awn = '2';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        e = (int)Console.Read();
                        if (e == awn)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '4':
                    {
                        int r, awns = '6';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        r = (int)Console.Read();
                        if (r == awns)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '5':
                    {
                        int t, awnse = '9';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        t = (int)Console.Read();
                        if (t == awnse)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case 'f':
                    {
                        Console.WriteLine("Happy fathers day tat!");
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Thats.....not valid...");
                        break;
                    }
            }
        }
    }
But after you type 1 or 2 or whatever and it says to guess a number it doesnt stop to let you guess, it just keeps going on to the end.
Anyone know the problem?
EDIT: Tat means dad in his language
Use Console.ReadLine() not Read() like: l = char.Parse(Console.ReadLine()); or l = Console.ReadLine()[0];
Read() will also read the carriage return, which is why it wasn't work right.
Quote Originally Posted by xDango View Post
Use Console.ReadLine() not Read() like: l = char.Parse(Console.ReadLine()); or l = Console.ReadLine()[0];
Read() will also read the carriage return, which is why it wasn't work right.
Oh, yea I figured it out thanks anyway /req close
Quote Originally Posted by Gruntlyon View Post
So I wrote this code:
Code:
class Program
    {
        static void Main(string[] args)
        {
            char l;
            Console.WriteLine("Press 1, 2, 3, 4, or 5. OR you can press 'f' for something special!");
            l = (char)Console.Read();
            switch (l)
            {
                case '1':
                    {
                        int q, a = '7';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        q = (int)Console.Read();
                        if (q == a)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }

                case '2':
                    {
                        int w, aw = '4';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        w = (int)Console.Read();
                        if (w == aw)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '3':
                    {
                        int e, awn = '2';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        e = (int)Console.Read();
                        if (e == awn)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '4':
                    {
                        int r, awns = '6';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        r = (int)Console.Read();
                        if (r == awns)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case '5':
                    {
                        int t, awnse = '9';
                        Console.WriteLine(" (Followed by *** ENTER ***)Enter a number bewtween 0 and 9:");
                        t = (int)Console.Read();
                        if (t == awnse)
                        {
                            Console.WriteLine("Correct!");
                        }
                        break;
                    }
                case 'f':
                    {
                        Console.WriteLine("Happy fathers day tat!");
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Thats.....not valid...");
                        break;
                    }
            }
        }
    }
But after you type 1 or 2 or whatever and it says to guess a number it doesnt stop to let you guess, it just keeps going on to the end.
Anyone know the problem?
EDIT: Tat means dad in his language

(int)Console.Readline() is invalid, you only can (or you should) convert text to number with:
int.Parse(Console.ReadLine())
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?