Console key example

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R3VzIENodWNo?=

    #1

    Console key example

    Does anyone have a simple example on how to use the Console.Key() function?
    I’m just trying to read in 5 numbers.
    Something like this.

    String Key ConsoleKeyInfo( );
    Int Number =0;
    For (int I =0; I<=5; I++)
    { Key = Console.Key();
    Number = Number & Convert.ToInt32 (Key);
    }

    --
    thank You
  • Doug Semler

    #2
    Re: Console key example

    "Gus Chuch" <GusChuch@discu ssions.microsof t.comwrote in message
    news:E4857153-AF72-4A68-B12D-10CD4B923492@mi crosoft.com...
    Does anyone have a simple example on how to use the Console.Key()
    function?
    I’m just trying to read in 5 numbers.
    Something like this.
    >
    String Key ConsoleKeyInfo( );
    Int Number =0;
    For (int I =0; I<=5; I++)
    { Key = Console.Key();
    Number = Number & Convert.ToInt32 (Key);
    }
    >

    Well, it would help if it was actually C# code. ConsoleKeyInfo is not a
    funciton, it is a type:
    ConsoleKeyInfo Key;

    At the end of the function, you'll wind up with Number equalling the value 0
    (because you assigned it 0 at the start and are doing bitwise &. 0 &
    number = 0).
    What you probably want to do is something like:
    List<intNumbers = new List<int>();
    .... in the loop:
    Numbers.Add(rea dValue);

    There is no function called Key() in the Console class you are probably
    looking for ReadKey.

    Convert.ToInt32 won't do what you're expecting with the ConsoleKeyInfo
    class....

    ReadKey reads one key at a time...what if I wanted to input a multidigit
    number (like "12")?

    --
    Doug Semler, MCPD
    a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
    The answer is 42; DNRC o-
    Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
    erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

    Comment

    Working...