Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Read is being ignored [duplicate]

Tags:

c#

I'm new to the C# language, and have only started learning it for use on the XNA Game Studio for X-box.

I have some minor experience with Java and C++, so I'm not a TOTAL noob. That's exactly why this problem is so frustrating to me.

I have created a simple code designed to add two numbers input from the user. Extremely simple stuff, but a good first step for any new language I feel.

I've declared my variables, and was trying to use Console.Read() to get numbers from the user to add. So far, the code outputs the message I want, then stops and reads in a single input from the user. After that, it messes up. The console outputs the next message, reads some random number (no input), then adds them together and outputs that instantly.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Add
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the first number to add: ");
            int firstNumber = Console.Read();

            Console.WriteLine("Please enter the second number to add: ");
            int secondNumber = Console.Read();

            int Sum = firstNumber + secondNumber;
            Console.WriteLine("The total of the two numbers is: " + Sum);

        }
    }
}

Sample runs:

Please enter the first number to add:

2

Please enter the second number to add:

The total of the two numbers is: 63


Please enter the first number to add:

3

Please enter the second number to add:

The total of the two numbers is: 64


It continues like that, acting as though the secondNumber is 61.

Thanks in advance for any help!

like image 650
Mitchell Thomas McCann Avatar asked Apr 25 '12 15:04

Mitchell Thomas McCann


4 Answers

That's because it is reading the next character from the console and then converting it to int, which gives the ASCII value, not the numeric value. So typing 2 will be interpreted as the character '2', with the ascii code 50. Try this instead:

int firstNumber = Int32.Parse(Console.ReadLine());
like image 58
Tudor Avatar answered Nov 12 '22 16:11

Tudor


Console.Read reads a single character. So when you enter "2" and hit Enter, you're supplying (1) the character '2', whose ASCII value is 50, and then (2) the carriage-return character, whose ASCII value is 13. The sum of these is ... 63. :-)

like image 41
Gareth McCaughan Avatar answered Nov 12 '22 16:11

Gareth McCaughan


In addition to what Gareth said, maybe the MSDN information will clear up to you why it's not waiting for your input on the second Console.Read() method:

Console.Read()

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

So, on your first Read() it's happily allowing you to enter whatever you want until you hit Enter

Then, it gets to the second Console.Read() and says, "Hey, I already have those characters from the first Console.Read() to go through. It just happens that the second one is whitespace (the carriage return)" and it assigns that whitespace ASCII value to secondNumber.

like image 3
Jesse Avatar answered Nov 12 '22 16:11

Jesse


The problem is that Console.Read() reads the first Return keypress and sends that to the second Console.Read() call. Your code should use ReadLine() instead and look something like this:

Console.WriteLine("Please enter the first number to add: ");
int firstNumber = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Please enter the second number to add: ");
int secondNumber = Convert.ToInt32(Console.ReadLine());
like image 1
Malice Avatar answered Nov 12 '22 16:11

Malice