Jump to content

Bug in compiler?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
ok this is very wierd for me...this was working before but now it is not working...i'm really confused

this is the code:

MainProgram.cs (here is entry point)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace TrashManager

{

    class MainProgram

    {

        static void Main(string[] args)

        {

            Menu meni = new Menu();

            meni.Start();

        }

    }

}


Menu.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace TrashManager

{

    class Menu

    {

        public void WriteMenuText()

        {

            Console.WriteLine("----------------------------------------");

            Console.WriteLine("--------------PROGRAM MENU--------------");

            Console.WriteLine("----------------------------------------");

            Console.WriteLine("Whole Numbers with for               : 1");

            Console.WriteLine("Floating point Numbers with while    : 2");

            Console.WriteLine("Currency Converter with Do-While loop: 3");

            Console.WriteLine("Waste schedule                       : 4");

            Console.WriteLine("Exit the program                     : 0");

            Console.WriteLine("----------------------------------------");

            Console.ReadKey();

        }


        public int Start()

        {

            WriteMenuText();

            try

            {

                int a = 5;


                while (a != 0)

                {

                    a = Convert.ToInt16(Console.Read());


                    switch (a)

                    {

                        case 1:

                            Console.WriteLine("Program nije dovršen");


                            break;

                        case 2:

                            Console.WriteLine("Program nije dovršen");

                            break;

                        case 3:

                            Console.WriteLine("Program nije dovršen");

                            

                            break;

                        case 4:

                            Console.WriteLine("Program nije dovršen");

                            break;

                        case 0:

                        default:

                            Console.WriteLine("Krivi unos...ponovite postupak");

                            

                            break;

                    }

                }

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                Console.ReadKey();


            }

            return 0;

        }

    }

}


what I'm getting here is: ImageShack® - Online Photo and Video Hosting

this is confusing me because i have never had any problems with switch

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
In WriteMenuText():

Tonchi said:



            Console.ReadKey();


Shouldn't you be capturing the return value of this method? By disregarding it, you're emptying the input buffer and causing this line to return -1 every time:

Tonchi said:


                    a = Convert.ToInt16(Console.Read());


Edit: I did some searching for you. The .net documentation for Console.Read() says this:

"The ReadLine method, or the KeyAvailable property and ReadKey method are preferable to using the Read method."

Source: Console.ReadKey Method (System)

I would avoid Console.Read() if I were you and just stick with Console.ReadKey().
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users