2

I am fairly new to programming so have some mercy ;) I am trying to build a program that can solve equations and give gradient and so on in c#, so I can make it more complex gradually. Problem is, there appears to be a wrong value from my input when I tried to start building it.

Console: Given value for "a": 9 The Output: 57

My Code:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Input an linear Eqasion in the following Pattern -- a * x + b");
            Console.Write("Given value for \"a\":");
            decimal aValue;
            aValue = Console.Read();
            Console.Write(aValue);
        }
    }
}
Vale
  • 23
  • 4

2 Answers2

5

Console.Read() returns an int, but not in the way you think. It returns the numeric value of the typed character, not the human-intuitive interpretation of a character that coincidentally happens to be a number. Consider for example what it would return if you type a letter, or any other non-numeric character.

And what is the numeric (decimal) value for the character '9'? 57 is.

It sounds like you want to read the line, not the character. For example:

string aValue;
aValue = Console.ReadLine();
Console.Write(aValue);

Remember that you'll need to press return to send the line to the application.

If you'll later need the value to be numeric, you'll still want to input the string but will want to parse it. For example:

string aValue;
aValue = Console.ReadLine();
if (decimal.TryParse(aValue, out decimal numericValue)
{
    Console.Write(numericValue);
}
else
{
    // The value could not be parsed as a decimal, handle this case as needed
}
David
  • 208,112
  • 36
  • 198
  • 279
  • Why do u need to declare aValue as a string first ? – Vale Feb 13 '20 at 12:33
  • @Vale: Because `Console.ReadLine()` returns a `string`. C# is statically typed, if you try to store a value in a variable of the wrong type then you'll see a compiler error. – David Feb 13 '20 at 12:33
  • `string aValue = Console.ReadLine();` would be better. – CompanyDroneFromSector7G Feb 13 '20 at 12:37
  • 2
    @CompanyDroneFromSector7G: Agreed, I was just keeping consistent with the OP's existing coding style. Personally I'd even just use `var` because to me `ReadLine` already carries the clear semantics of being a string. – David Feb 13 '20 at 12:38
  • And here's me learning you can declare an out variable in the function call. Boy do I miss c# sometimes, VB just isn't the same :( This should definitely be marked as the answer, 10/10 explanation. – Zaelin Goodman Feb 13 '20 at 12:38
4

Console.Read returns the character code entered on the command line in this scenario. The ASCII character code of 9 is 57. If you're wanting numeric input, you'd be better using Console.ReadLine with Decimal.Parse (or better yet, Decimal.TryParse)

It is also worth noting that Console.Read only returns one character at a time, meaning for any inputs past 1 digit you'll need special handling. I highly recommend using ReadLine and parsing the string over handling converting the character codes to the number they represent.

Zaelin Goodman
  • 896
  • 4
  • 11