none
input methods RRS feed

  • Question

  • string myName; int myVar1, myVar2, myVar3; Console.WriteLine("Please enter your name: "); myName = (Console.ReadLine()); Console.WriteLine("\n Please enter your number"); myVar1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\n Please enter your character"); myVar2 = Convert.ToInt32(Console.Read()); Console.WriteLine("\n Enter your ascii:"); myVar3 = (Console.Read()); char myCharVar = (Char)myVar3; Console.WriteLine("\n Hello {0}", myName); Console.WriteLine("\n My number is {0}\n My char is {1}",myVar1,myVar2); Console.WriteLine("my ascii is {0}", myVar3,myCharVar); Console.ReadKey();


    Thursday, December 21, 2017 9:19 PM

All replies

  • What are you trying to do?
    From what I see I can tell you may have issues if the user does not input something that can be cast to that data type like a letter where requested for a number.


    Dim Blondedude092 as SkypeUser

    Thursday, December 21, 2017 9:23 PM
  • Please edit this and make it a discussion. Or ask a question.


    Sam Hobbs
    SimpleSamples.Info

    Friday, December 22, 2017 2:08 AM
  • Hello shariiif,

    You issues is related to Console.Read method, which just only take one character from input stream. The input stream will left "\r\n" characters that not been handing. The next Console.Read will hit "\r" immediately rather than wait user input. The JediJohn82 has better explanation with it. As for workaround, you could try the below code.

    ...
    Console.WriteLine("\n Please enter your character");
    myVar2 = Convert.ToInt32(Console.Read());
    
    Console.Read();
    Console.Read();
    
    Console.WriteLine("\n Enter your ascii:");
    myVar3 = (Console.Read());
    ...

    OR

    ...  
    Console.WriteLine("\n Please enter your character");
    myVar2 = Convert.ToInt32(Console.ReadLine()[0]);
    ...

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, December 27, 2017 6:38 AM
    Friday, December 22, 2017 3:06 AM
    Moderator