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??

Vithal WadjeRank #19 · 47.2K reputationPosted Sep 28, 2014, 7:16 AM EDT
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
Vithal WadjeRank #19 · 47.2K reputationPosted Sep 28, 2014, 6:50 AM EDT
Aravind Chitra11 reputationPosted Sep 28, 2014, 6:55 AM EDT
Vithal WadjeRank #19 · 47.2K reputationPosted Sep 28, 2014, 6:58 AM EDT
Vithal WadjeRank #19 · 47.2K reputationPosted Sep 28, 2014, 7:02 AM EDT
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();
Aravind Chitra11 reputationPosted Sep 28, 2014, 7:05 AM EDT
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
Aravind Chitra11 reputationPosted Sep 28, 2014, 7:07 AM EDT
Aravind Chitra11 reputationPosted Sep 28, 2014, 7:23 AM EDT
Vithal WadjeRank #19 · 47.2K reputationPosted Sep 28, 2014, 7:28 AM EDT
Aravind Chitra11 reputationPosted Sep 28, 2014, 7:32 AM EDT
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 29, 2014, 2:46 AM EDT
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
Ajay YadavRank #313 · 8.1K reputationPosted Oct 26, 2014, 1:21 AM EDT
MahaRank #2,573 · 543 reputationPosted Oct 26, 2014, 2:42 AM EDT
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();
}
}