Console.WriteLine is printing out different values from what i entered [duplicate]
This question already has an answer here:
How can I read user input from the console?
8 answers
So i entered this code
static void Main(string args)
int y = Console.Read();
Program program = new Program();
program.Prime(y);
public void Prime(int Value)
Console.WriteLine(Value);
and when i enter a value, what prints out is the value I entered + 48.
So if I enter 3, 'Console.WriteLine' prints out 51. Please help. I thought it was from my laptop so i restarted it but still no luck.
c# .net
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 22:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I read user input from the console?
8 answers
So i entered this code
static void Main(string args)
int y = Console.Read();
Program program = new Program();
program.Prime(y);
public void Prime(int Value)
Console.WriteLine(Value);
and when i enter a value, what prints out is the value I entered + 48.
So if I enter 3, 'Console.WriteLine' prints out 51. Please help. I thought it was from my laptop so i restarted it but still no luck.
c# .net
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 22:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
asciitable.com also the documentation for Console.ReadLine Method especially:public static string ReadLine ();
- it returns string, not int
– WelcomeOverflow
Nov 13 '18 at 22:39
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45
add a comment |
This question already has an answer here:
How can I read user input from the console?
8 answers
So i entered this code
static void Main(string args)
int y = Console.Read();
Program program = new Program();
program.Prime(y);
public void Prime(int Value)
Console.WriteLine(Value);
and when i enter a value, what prints out is the value I entered + 48.
So if I enter 3, 'Console.WriteLine' prints out 51. Please help. I thought it was from my laptop so i restarted it but still no luck.
c# .net
This question already has an answer here:
How can I read user input from the console?
8 answers
So i entered this code
static void Main(string args)
int y = Console.Read();
Program program = new Program();
program.Prime(y);
public void Prime(int Value)
Console.WriteLine(Value);
and when i enter a value, what prints out is the value I entered + 48.
So if I enter 3, 'Console.WriteLine' prints out 51. Please help. I thought it was from my laptop so i restarted it but still no luck.
This question already has an answer here:
How can I read user input from the console?
8 answers
c# .net
c# .net
edited Nov 13 '18 at 22:40
Alex Sikilinda
2,194925
2,194925
asked Nov 13 '18 at 22:38
Chi ChiChi Chi
4
4
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 22:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 22:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
asciitable.com also the documentation for Console.ReadLine Method especially:public static string ReadLine ();
- it returns string, not int
– WelcomeOverflow
Nov 13 '18 at 22:39
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45
add a comment |
asciitable.com also the documentation for Console.ReadLine Method especially:public static string ReadLine ();
- it returns string, not int
– WelcomeOverflow
Nov 13 '18 at 22:39
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45
asciitable.com also the documentation for Console.ReadLine Method especially:
public static string ReadLine ();
- it returns string, not int– WelcomeOverflow
Nov 13 '18 at 22:39
asciitable.com also the documentation for Console.ReadLine Method especially:
public static string ReadLine ();
- it returns string, not int– WelcomeOverflow
Nov 13 '18 at 22:39
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45
add a comment |
3 Answers
3
active
oldest
votes
You want
Console.ReadLine();
Read is the next character
add a comment |
Because the ascii code for
'0' is 48
'1' is 49
.
.
.
'9' is 57
If you enter 1 for example, it is not actually number 1, but '1' (character with ascii code of 49) and parsing it as int would give you 49.
You could do this to get the desired result:
int y = Console.Read() - '0';
However if you want to read numbers more than 9 (more than one digit) this ain't gonna work, it is better that you parse it to int:
int y = int.Parse(Console.ReadLine();
Or even to make sure that entered value is parseable to int:
int y = 0;
while(!int.TryParse(Console.ReadLine(), y);
add a comment |
It is trying to pring the ascii value of the string, where 3 = 51. Console.Read()
is returning the ascii value because you have it as an int
. To fix it, use string
instead -- there is no reason to use int
here when you are not doing anything with it.
static void Main(string args)
string y = Console.Read(); // changed int to string
Program program = new Program();
program.Prime(y);
public void Prime(string Value) // changed into to string
Console.WriteLine(Value);
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want
Console.ReadLine();
Read is the next character
add a comment |
You want
Console.ReadLine();
Read is the next character
add a comment |
You want
Console.ReadLine();
Read is the next character
You want
Console.ReadLine();
Read is the next character
answered Nov 13 '18 at 22:42
Ryan SchlueterRyan Schlueter
1,8121818
1,8121818
add a comment |
add a comment |
Because the ascii code for
'0' is 48
'1' is 49
.
.
.
'9' is 57
If you enter 1 for example, it is not actually number 1, but '1' (character with ascii code of 49) and parsing it as int would give you 49.
You could do this to get the desired result:
int y = Console.Read() - '0';
However if you want to read numbers more than 9 (more than one digit) this ain't gonna work, it is better that you parse it to int:
int y = int.Parse(Console.ReadLine();
Or even to make sure that entered value is parseable to int:
int y = 0;
while(!int.TryParse(Console.ReadLine(), y);
add a comment |
Because the ascii code for
'0' is 48
'1' is 49
.
.
.
'9' is 57
If you enter 1 for example, it is not actually number 1, but '1' (character with ascii code of 49) and parsing it as int would give you 49.
You could do this to get the desired result:
int y = Console.Read() - '0';
However if you want to read numbers more than 9 (more than one digit) this ain't gonna work, it is better that you parse it to int:
int y = int.Parse(Console.ReadLine();
Or even to make sure that entered value is parseable to int:
int y = 0;
while(!int.TryParse(Console.ReadLine(), y);
add a comment |
Because the ascii code for
'0' is 48
'1' is 49
.
.
.
'9' is 57
If you enter 1 for example, it is not actually number 1, but '1' (character with ascii code of 49) and parsing it as int would give you 49.
You could do this to get the desired result:
int y = Console.Read() - '0';
However if you want to read numbers more than 9 (more than one digit) this ain't gonna work, it is better that you parse it to int:
int y = int.Parse(Console.ReadLine();
Or even to make sure that entered value is parseable to int:
int y = 0;
while(!int.TryParse(Console.ReadLine(), y);
Because the ascii code for
'0' is 48
'1' is 49
.
.
.
'9' is 57
If you enter 1 for example, it is not actually number 1, but '1' (character with ascii code of 49) and parsing it as int would give you 49.
You could do this to get the desired result:
int y = Console.Read() - '0';
However if you want to read numbers more than 9 (more than one digit) this ain't gonna work, it is better that you parse it to int:
int y = int.Parse(Console.ReadLine();
Or even to make sure that entered value is parseable to int:
int y = 0;
while(!int.TryParse(Console.ReadLine(), y);
edited Nov 13 '18 at 22:55
answered Nov 13 '18 at 22:42
Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani
20.3k1565114
20.3k1565114
add a comment |
add a comment |
It is trying to pring the ascii value of the string, where 3 = 51. Console.Read()
is returning the ascii value because you have it as an int
. To fix it, use string
instead -- there is no reason to use int
here when you are not doing anything with it.
static void Main(string args)
string y = Console.Read(); // changed int to string
Program program = new Program();
program.Prime(y);
public void Prime(string Value) // changed into to string
Console.WriteLine(Value);
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
add a comment |
It is trying to pring the ascii value of the string, where 3 = 51. Console.Read()
is returning the ascii value because you have it as an int
. To fix it, use string
instead -- there is no reason to use int
here when you are not doing anything with it.
static void Main(string args)
string y = Console.Read(); // changed int to string
Program program = new Program();
program.Prime(y);
public void Prime(string Value) // changed into to string
Console.WriteLine(Value);
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
add a comment |
It is trying to pring the ascii value of the string, where 3 = 51. Console.Read()
is returning the ascii value because you have it as an int
. To fix it, use string
instead -- there is no reason to use int
here when you are not doing anything with it.
static void Main(string args)
string y = Console.Read(); // changed int to string
Program program = new Program();
program.Prime(y);
public void Prime(string Value) // changed into to string
Console.WriteLine(Value);
It is trying to pring the ascii value of the string, where 3 = 51. Console.Read()
is returning the ascii value because you have it as an int
. To fix it, use string
instead -- there is no reason to use int
here when you are not doing anything with it.
static void Main(string args)
string y = Console.Read(); // changed int to string
Program program = new Program();
program.Prime(y);
public void Prime(string Value) // changed into to string
Console.WriteLine(Value);
answered Nov 13 '18 at 22:52
Tanner H.Tanner H.
1069
1069
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
add a comment |
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
What i did was int y = Convert.ToInt32(Console.ReadLine());
– Chi Chi
Dec 3 '18 at 13:27
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
and it worked quite well
– Chi Chi
Dec 3 '18 at 13:28
add a comment |
asciitable.com also the documentation for Console.ReadLine Method especially:
public static string ReadLine ();
- it returns string, not int– WelcomeOverflow
Nov 13 '18 at 22:39
Its printing asccii value of 3.
– Ashif Nataliya
Nov 13 '18 at 22:45