Home > Net >  Error in a Sum Calculation in C# Visual Studio 2022
Error in a Sum Calculation in C# Visual Studio 2022

Time:07-03

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!

.........................

Update:

I had to convert int to string through int.Parse, now the calculation worked:

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

CodePudding user response:

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.

  • Related