hello all,
the following code is not working as expected
CODE
Console.WriteLine("enter alphabet");
int c = Console.Read();
Console.WriteLine(c);
Console.WriteLine("enter string");
string j = Console.ReadLine();
Console.WriteLine(j);
Console.ReadKey();
it doesn't allow to enter the string and just exits the program
but if code is reversed,it works fine
Console.WriteLine("enter string");
string j = Console.ReadLine();
Console.WriteLine(j);
Console.WriteLine("enter alphabet");
int c = Console.Read();
Console.WriteLine(c);
Console.ReadKey();
can anyone explain this??
MahaPosted Oct 26, 2014, 6:42 AM
http://www.dotnetperls.com/console-read
'Note: When the Read method is first called, it blocks execution waiting for the input to be sent by the terminal window'.
You can alter the program as follows:
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter alphabet ");
char c = char.Parse(Console.ReadLine());
int x = Convert.ToInt32(c);
Console.WriteLine(x);
Console.Write("Enter string ");
string j = Console.ReadLine();
Console.WriteLine(j);
Console.ReadKey();
}
}
Ajay YadavPosted Oct 26, 2014, 5:21 AM
Wim SturkenboomPosted Sep 29, 2014, 6:46 AM
Console.Read() reads one character from the keyboard buffer; but the buffer contains both the character(s) that you typed and the
Next you're reading input again (using Console.ReadLine()); this reads the buffer up to (and including) the
Note that Console.ReadLine() will strip the
You can test by typing abc
// edit: correct little mistake in first paragraph
Aravind ChitraPosted Sep 28, 2014, 11:32 AM
Vithal WadjePosted Sep 28, 2014, 11:28 AM
Aravind ChitraPosted Sep 28, 2014, 11:23 AM
Vithal WadjePosted Sep 28, 2014, 11:16 AM
Console.WriteLine("enter alphabet");
int c = Console.Read();
Console.WriteLine(c);
Console.ReadLine();
Console.WriteLine("Enter string");
string j = Console.ReadLine();
Console.WriteLine("Your Entered String is :"+j);
Console.ReadLine();
this is output
Aravind ChitraPosted Sep 28, 2014, 11:07 AM
Aravind ChitraPosted Sep 28, 2014, 11:05 AM
enter alphabet
a
97
enter string
then it exits
in the second case,the output is like
enter string
haha
haha
enter alphabet
a
97
Vithal WadjePosted Sep 28, 2014, 11:02 AM
Console.WriteLine("enter alphabet");
int c = Console.Read();
Console.WriteLine(c);
Console.WriteLine("enter string");
string j = Console.ReadLine();
Console.ReadLine();
Console.WriteLine(j);
Console.ReadKey();
Console.ReadLine();
Vithal WadjePosted Sep 28, 2014, 10:58 AM
Aravind ChitraPosted Sep 28, 2014, 10:55 AM
Vithal WadjePosted Sep 28, 2014, 10:50 AM