Console.Read() not working fine.

Posted by Prakashrajmca under C# on 8/29/2011 | Points: 10 | Views : 1249 | Status : [Member] | Replies : 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

int web = Console.Read();
Console.WriteLine(web);
Console.Read();
}
}
}

the above code working as strange i could not understand..
if give input 12 it prints the output 49, why it gives wrong integer..(new to dotnet)..

Thanks




Responses

Posted by: Dhirendra on: 8/29/2011 [Member] Starter | Points: 25

Up
0
Down
Please note that console.read blocks return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input
so you have to take character by character input through read method then convert it to INT bu checking white space...
otherwise use Console.ReadLine()


Prakashrajmca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/29/2011 [Member] Starter | Points: 25

Up
0
Down
see this below link for more info..
http://msdn.microsoft.com/en-us/library/system.console.read.aspx

Prakashrajmca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prakashrajmca on: 8/29/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Now i got it partially the Console.Read() only reads a single character and when i try to print it prints the ascii values..

1) Is Console.Read() only reads a single character?

2) Why it displays ascii values?

Thanks in Advance..

Prakashrajmca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response