473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Console.Read() Problem

I am having problem with Console.Read(), the Value I
entered is not the value that store in my variable.

public class Num
{
int num;
Console.Write("Enter a number: ");
num=Console.Read();
Console.WriteLine("{0}",num)

}

every time i test this code, I enter 1, but the value
store in num is 49, which mean i got 49 as output, i
enter 2, I got 50. does anyone know, why this happen and
any solutions for it. it sounds like the program take 1
and converted to ascII code, and stored at num. Help
please, thank you.
Nov 15 '05 #1
3 8278
Hi,

That is the correct behaviour, Console.Read() returns the character entered,
in your case the character code for 1 is correctly returned as 49. If you
want the number you can either subtract 48 (I am not really recommending
this!). You could use ReadLine and use Int32.Parse() or Convert functions to
convert from the input string to an integer.. If you can be more specific as
to your requirements myself or someone else might be able to give a more
specific solution.

Hope this helps

Chris Taylor

"Jerry" <an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...
I am having problem with Console.Read(), the Value I
entered is not the value that store in my variable.

public class Num
{
int num;
Console.Write("Enter a number: ");
num=Console.Read();
Console.WriteLine("{0}",num)

}

every time i test this code, I enter 1, but the value
store in num is 49, which mean i got 49 as output, i
enter 2, I got 50. does anyone know, why this happen and
any solutions for it. it sounds like the program take 1
and converted to ascII code, and stored at num. Help
please, thank you.

Nov 15 '05 #2
Hi Jerry,

Console.Read() usually returns the input character, which in your
case is the ASCII value of the interger that you entered.
Please try reading the input as a string and then use one of the
Convert.XXX functions to convert to your required type.

Console.Write("Enter a number: ");
String numAsString = Console.ReadLine();
Console.WriteLine("Number is {0}", Convert.ToInt32(numAsString));

Hope this helps.

Regards,
Aravind C
"Jerry" <an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...
I am having problem with Console.Read(), the Value I
entered is not the value that store in my variable.

public class Num
{
int num;
Console.Write("Enter a number: ");
num=Console.Read();
Console.WriteLine("{0}",num)

}

every time i test this code, I enter 1, but the value
store in num is 49, which mean i got 49 as output, i
enter 2, I got 50. does anyone know, why this happen and
any solutions for it. it sounds like the program take 1
and converted to ascII code, and stored at num. Help
please, thank you.

Nov 15 '05 #3
Well, Console.Read returns an int representing the character entered.
A couple of possibilities are

public class Num
{
int num;
Console.Write( "Enter a number: " );
num = Console.Read();
if(num < 58 & num > 47)
{
num -= 48;
Console.WriteLine( "{0}", num )
// I prefer WriteLine( num );
}
}

public class Num
{
int num;
Console.Write( "Enter a number: " );
num = Console.Read();
if(num < 58 & num > 47)
{
Console.WriteLine( "{0}", (char)num )
}
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Dom | last post by:
can someone please help me display text in the console cout << "Testing"; in a different colour to the default one
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
2
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
6
by: JM | last post by:
Hi, I need a function for a character based program (console) that works exactly like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then move on. Console.Read;...
12
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
7
by: mabra | last post by:
Hi All ! Problem:Reading/duplicating "Console.In" fails for unknown reason. I wrote a little console helper tool, named TEE, which just duplicates the StdIn to the StdOut AND an additional...
2
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.