10

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!

Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Related post - [Console.Read() and Console.ReadLine() problems](https://stackoverflow.com/q/12308098/465053) – RBT May 24 '18 at 08:58

8 Answers8

11

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());
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • 1
    +1 for a solution to the problem instead of only an explanation. – Marc Apr 25 '12 at 15:39
  • Thank you for this. I didn't know it returned the ASCII code. To the others saying use Console.ReadLine(), I've tried that, but it returned an error: cannot complicitly convert type 'string' to 'int' I guess I was being hopeful for thinking this would work. XD Thanks again for the very detailed explanation! – Mitchell Thomas McCann Apr 25 '12 at 15:39
  • @Mitchell Thomas McCann. But this method with `Int32.Parse` does not work for you? – Tudor Apr 25 '12 at 15:41
  • Just substituted it with the old code, and it works perfectly. Thank you! – Mitchell Thomas McCann Apr 25 '12 at 15:43
8

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. :-)

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • Didn't know that about the ASCII. Thought it just returned what was entered before a whitespace was read. Thanks! – Mitchell Thomas McCann Apr 25 '12 at 15:41
  • @MitchellThomasMcCann Since it's already tripped you up, I'd suggest a quick skim over the Wikipedia article on ASCII, and a [reference table](http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters) on that article of what characters map to what ASCII codes. (The DEC and Glyph columns are relevant here) – Izkata Apr 25 '12 at 20:42
3

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.

Jesse
  • 307
  • 1
  • 5
  • Thanks. I read that, however, I thought that meant: Read entire string of numbers on first call. Read single number on second call. Repeat. i.e, multiple stops to get data. Thank you for the clarification! – Mitchell Thomas McCann Apr 25 '12 at 15:50
1

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());
Malice
  • 3,927
  • 1
  • 36
  • 52
1

Console.Read reads a single character from the input block. If you enter a number and then press the enter key it will read the enter key, or the next digit of the first number you entered.

You will likely want to use Console.ReadLine instead.

Mike Calvert
  • 131
  • 7
0

I think you want the Console.ReadLine() Method

Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
0

You should Try Console.ReadLine();

csteinmueller
  • 2,427
  • 1
  • 21
  • 32
0

You probably want ReadLine not Read as Read takes the next character in the stream, but ReadLine will wait for the user to press enter.

This would cause a bug if your user type 34 the first time, because firstNumber equal 3 not 34.

taylonr
  • 10,732
  • 5
  • 37
  • 66