0

I'm doing a sum of two numbers exercise in Visual Studio 2022, in C# language.

Here is the current code:

class Program
{
    static void Main(string[] args)
    {
        int x, y, sum;

        Console.Write("Enter the value of X: ");
        x = Console.Read();
        Console.Write("Enter the value of Y: ");
        y = Console.Read();
        Console.WriteLine();

        sum = x + y;

        Console.WriteLine("SUM = " + sum);
        Console.WriteLine();
        Console.WriteLine("Press any key to close...");
        Console.ReadKey();
    }
}

It's happening that after typing a value for x and pressing ENTER, it's jumping straight to the sum calculation and giving me an inexplicable result of 66, but the correct thing would be to expect me to type the value of y and then add the two values.

here's a print of the console:

enter image description here

I'm not understanding this error, I would like an explanation of what is wrong in the code!

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Marcb7
  • 79
  • 1
  • 7
  • 1
    I've removed the visual-studio tag because this isn't a question about using the Visual Studio application. Please read tag descriptions before you use them. – ProgrammingLlama Jul 03 '22 at 04:24
  • 3
    I expect you want [`ReadLine`](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=net-6.0) rather than [`Read`](https://learn.microsoft.com/en-us/dotnet/api/system.console.read?view=net-6.0) – ProgrammingLlama Jul 03 '22 at 04:26
  • try the number 45 as your X value .... – rene Jul 03 '22 at 04:27
  • 1
    5 has ASCII code 53. When you press enter on Windows, it produces carriage return (ASCII 13) and line feed (ASCII 10). Your code is showing the total 66, which is 53+13. You need to read the user input as a `string` and parse it to an integer. – ProgrammingLlama Jul 03 '22 at 04:31
  • Well, I had to use int.Parse to convert int to string, I didn't understand the need for this conversion for everything to work, but at least now it worked! – Marcb7 Jul 03 '22 at 04:45
  • There is no question in the post - did you plan to ask one? Very confusing post. – Alexei Levenkov Jul 03 '22 at 06:33

1 Answers1

3

Console.Read is extracting a single character from the input. When you exctract a character into an int variable, the int will hold the ascii value of the character.

In your case, you entered '5' and then ENTER (which is translated in Windows to carriage-return + linefeed).
Your 1st Read() extracted the character '5' and the 2nd extracted a carriage-return character.
'5' has an ascii value of 53, and the carriage-return character has the ascii value of 13, and hence the result you see: 66.

Instead you should use Console.ReadLine. It extracts the whole input line into a string variable. This string can be converted into an int with int.Parse method.

Fixed version:

class Program
{
    static void Main(string[] args)
    {
        int x, y, sum;

        Console.Write("Enter the value of X: ");
        x = int.Parse(Console.ReadLine());
        Console.Write("Enter the value of Y: ");
        y = int.Parse(Console.ReadLine());
        Console.WriteLine();

        sum = x + y;

        Console.WriteLine("SUM = " + sum);
        Console.WriteLine();
        Console.WriteLine("Press any key to close...");
        Console.ReadKey();
    }
}

Output example:

Enter the value of X: 45
Enter the value of Y: 30

SUM = 75

Note: int.Parse can fail and throw an exception if the string does not containt a valid integer value. In your final code you should either handle such exceptions, or use the alternative: int.TryParse.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • Thanks for the solution. I managed to find the solution before I saw your answer, I hadn't seen your answer before, but thanks for your input! – Marcb7 Jul 03 '22 at 05:08
  • Answer to a post without question... Even otherwise it is just duplicate of https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline – Alexei Levenkov Jul 03 '22 at 06:34
  • @AlexeiLevenkov originally the post contained a question which I tried to answer as well as I could. The OP edited the question later on, and I commented above about it (https://stackoverflow.com/questions/72843897/error-in-a-sum-calculation-in-c-sharp-visual-studio-2022/72843952#comment128663145_72843897). BTW - I was not aware of the duplicate. – wohlstad Jul 03 '22 at 06:40