Console key example

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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);
}
 
Gus Chuch said:
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<int> Numbers = new List<int>();
.... in the loop:
Numbers.Add(readValue);

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top