This program is given in the following website:
http://www.dotnetperls.com/console-read
According to code while ((result = Console.Read()) != 0) program must end when input is 0, but it is not ending. Please explain the reason. Problem is highlighted.
using System;
class Program
{
static void Main()
{
int result;
while ((result = Console.Read()) != 0)
{
Console.WriteLine("{0} = {1}", result, (char)result);
}
Console.ReadKey();
}
}
Loading
VulpesPosted Jul 10, 2013, 10:38 AM
It is designed to read a line of input terminated by a carriage return or line feed and therefore returns a string. The string contains the characters input, excluding the terminating character, and these are removed from the console input buffer as they're read.
If the 0 key is pressed followed by carriage return, Console.ReadLine returns the string "0" and this can then be parsed to an integer. When this happens, the 'while' condition will no longer be satisfied and the loop will end.
In contrast, Console.Read is designed to read single characters but buffers them until the enter key is pressed. Subsequent calls to Console.Read then retrieve the characters one by one. However, it returns the integer equivalent of the character (i.e. its Unicode value) rather than the character itself. This is probably done so that the special value -1, which is returned when there are no further characters to be read, can be accomodated.
The value 0 corresponds to the null character '\0'. This, of course, isn't a normal keyboard character but after playing around a bit, I've found that you can generate it using CTRL @.
So, if you press that after entering dotnetperls and then press enter, your original program should work without the need for CTRL-Z.
Posted Jul 10, 2013, 4:09 PM
VulpesPosted Jul 10, 2013, 4:00 PM
carriage return ('\r')
line feed ('\n')
The ASCII or Unicode values of these characters are 13 and 10 respectively but, since they don't correspond to printable characters, nothing appears after the '=' sign.
Posted Jul 10, 2013, 2:36 PM
100 = d
111 = o
116 = t
110 = n
101 = e
116 = t
112 = p
101 = e
114 = r
108 = l
115 = s
13 =
10 =
Posted Jul 10, 2013, 10:10 AM
while ((result = Console.Read()) != 0) is not terminating the program when 0 is input. Please explain the reason.
VulpesPosted Jul 10, 2013, 9:58 AM
It appears that you have to press the enter key again after pressing Ctrl-Z to break the loop.
You then have to press a further key to satisfy Console.ReadKey.
VulpesPosted Jul 10, 2013, 9:37 AM
If you look at the documentation for Console.Read():
http://msdn.microsoft.com/en-us/library/system.console.read(v=vs.100).aspx
you'll see that it returns -1 (not 0) when there are no more characters to read. However, to generate the -1 value, you have to press Ctrl-Z otherwise it will keep on blocking.
So, you need to enter:
dotnetperls
press enter key
press Ctrl-Z
for the loop to end after changing the code to this:
using System;
class Program
{
static void Main()
{
int result;
while ((result = Console.Read()) != -1)
{
Console.WriteLine("{0} = {1}", result, (char)result);
}
Console.ReadKey();
}
}