Console.WriteLine is printing out different values from what i entered [duplicate]










0
















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.










share|improve this question















marked as duplicate by Servy .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

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















0
















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.










share|improve this question















marked as duplicate by Servy .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

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













0












0








0









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

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 .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

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

















  • 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












3 Answers
3






active

oldest

votes


















1














You want




Console.ReadLine();




Read is the next character






share|improve this answer






























    1














    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);





    share|improve this answer
































      0














      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);






      share|improve this answer























      • 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

















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You want




      Console.ReadLine();




      Read is the next character






      share|improve this answer



























        1














        You want




        Console.ReadLine();




        Read is the next character






        share|improve this answer

























          1












          1








          1







          You want




          Console.ReadLine();




          Read is the next character






          share|improve this answer













          You want




          Console.ReadLine();




          Read is the next character







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 22:42









          Ryan SchlueterRyan Schlueter

          1,8121818




          1,8121818























              1














              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);





              share|improve this answer





























                1














                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);





                share|improve this answer



























                  1












                  1








                  1







                  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);





                  share|improve this answer















                  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);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 22:55

























                  answered Nov 13 '18 at 22:42









                  Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani

                  20.3k1565114




                  20.3k1565114





















                      0














                      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);






                      share|improve this answer























                      • 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















                      0














                      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);






                      share|improve this answer























                      • 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













                      0












                      0








                      0







                      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);






                      share|improve this answer













                      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);







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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

















                      • 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



                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      Evgeni Malkin