none
C# User Input

    Question

  • I have been into C++ for a few months and got up to learning functions, pointers, references, and a little OOP. I wanted to experiment with C#, for many personal reasons. It seems to attract me more..

    With C++ all you have to do is..

    cout << "Enter a num";
    cin >> num;

    Now, in C#
    I don't know how to get a user input for a number.The Console.ReadLine() only does strings I believe. And I remember using something else..but when I entered '4' it output '52', input '5' i get '53'. Thats probably the ASCII codes? How can I just get it to output the number? An example of a "guess my number" program would be much appreciated Smile

    Marky03
    Saturday, May 19, 2007 1:41 PM

Answers

  • Just for future reference for readers reading this thread..I picked up a book today with another example.

    int num;
    Console.WriteLine("Enter a number: ");
    num = Convert.ToInt32(Console.ReadLine());


    Either example is good. I just prefer to use this one ;-)

    Marky03
    Saturday, May 19, 2007 9:03 PM
  • Hi mate,

     

    You just got to convert the string to a number using the double or int static TryParse method. Here an example of the ReadLine and ReadKey console input. 

     

     

     

    Code Snippet

    static void Main(string[] args)

    {

     

    Console.WriteLine("Please enter a number.");

     

     //ReadLine example

     

    string input = Console.ReadLine();

    double inputAsNumber;

    if (double.TryParse(input, out inputAsNumber) == true)

    {

    Console.WriteLine("You entered a valid number: {0}", inputAsNumber);

    }

    else

    {

    Console.WriteLine("You entered an invalid number");

    }

    Console.ReadLine();

     

     //ReadKey example

     

    Console.WriteLine("Please enter a number.");

    ConsoleKeyInfo key = Console.ReadKey();

    if (double.TryParse(key.KeyChar.ToString(), out inputAsNumber) == true)

    {

    Console.WriteLine("You entered a valid number: {0}", inputAsNumber);

    }

    else

    {

    Console.WriteLine("You entered an invalid number");

    }

    Console.ReadLine();

    }

     

    Saturday, May 19, 2007 2:11 PM

All replies

  • Hi mate,

     

    You just got to convert the string to a number using the double or int static TryParse method. Here an example of the ReadLine and ReadKey console input. 

     

     

     

    Code Snippet

    static void Main(string[] args)

    {

     

    Console.WriteLine("Please enter a number.");

     

     //ReadLine example

     

    string input = Console.ReadLine();

    double inputAsNumber;

    if (double.TryParse(input, out inputAsNumber) == true)

    {

    Console.WriteLine("You entered a valid number: {0}", inputAsNumber);

    }

    else

    {

    Console.WriteLine("You entered an invalid number");

    }

    Console.ReadLine();

     

     //ReadKey example

     

    Console.WriteLine("Please enter a number.");

    ConsoleKeyInfo key = Console.ReadKey();

    if (double.TryParse(key.KeyChar.ToString(), out inputAsNumber) == true)

    {

    Console.WriteLine("You entered a valid number: {0}", inputAsNumber);

    }

    else

    {

    Console.WriteLine("You entered an invalid number");

    }

    Console.ReadLine();

    }

     

    Saturday, May 19, 2007 2:11 PM
  • Thanks for your help! Smile
    Saturday, May 19, 2007 2:21 PM
  • Just for future reference for readers reading this thread..I picked up a book today with another example.

    int num;
    Console.WriteLine("Enter a number: ");
    num = Convert.ToInt32(Console.ReadLine());


    Either example is good. I just prefer to use this one ;-)

    Marky03
    Saturday, May 19, 2007 9:03 PM
  • What if the user pushes "enter" by accident before entering a number. What would happen? How would you fix that problem?
    Sunday, August 05, 2012 5:20 AM
  • This is a very old thread.

    But to answer your question anyway, use int.TryParse() instead of Convert.ToInt32().


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, August 05, 2012 10:53 PM
  • Just for future reference for readers reading this thread..I picked up a book today with another example.

    int num;
    Console.WriteLine("Enter a number: ");
    num = Convert.ToInt32(Console.ReadLine());


    Either example is good. I just prefer to use this one ;-)

    Marky03
    I like your version too. It's a lot more concise. 
    Saturday, December 15, 2012 3:19 PM
  • WRONG! 

    You need to use int.TryParse() instead of Convert.ToInt32(). If the string you're attempting to convert is NOT a number, Convert will throw an exception, whereas TryParse()  will not.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, December 15, 2012 11:50 PM