1

It would seem like this whole simple code is correct, but when I click start and type in any number such as 4 or 6, the only output is the "else if" every time!

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int c = Convert.ToInt32(Console.Read());
            if (c >= 0  && c < 5)
            {
                Console.Write("# is > 0");
            } 
            else if (c >= 5)
            {
                Console.Write("# is > 5");
            }
            Console.ReadKey();

        }
    }
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
DDJ
  • 807
  • 5
  • 13
  • 31

4 Answers4

5

Console.Read reads:

The next character from the input stream, [...]

If you look at the ASCII table, you will notice that characters representing digits have values starting from 48. The value you're getting is already an integer, so there is nothing to convert (see Console.Read signature in the documentation). As suggested by Travis J, you can use the following code, to get the expected result:

int c = Convert.ToInt32(Console.ReadLine());

As you may see in the documentation for Console.ReadLine - it returns a string.

Consider using the debugger, as suggested in the comments under your question, along with reading the documentation.

Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1

The problem here is that you are taking input as an ascii character. So you end up with the ascii value. For example, when 4 is entered, 52 is the integer representation of that character and that is the value returned from the .Read.

The value is a char value (int) because you are only using .Read. If you change it to .ReadLine it will come in as a string and properly cast to an int.

int c = Convert.ToInt32(Console.ReadLine());
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • Technically, it actually returns an `int` (not `char` https://msdn.microsoft.com/en-us/library/system.console.read%28v=vs.110%29.aspx) Granted, that int is the ASCII value of the character :) – BradleyDotNET Sep 25 '15 at 22:23
1

Using Console.Read is the problem here. It returns an int that represents the ASCII value of the next key that is pressed. For the character '4' for example, that's 52. Convert.ToInt32 will just return the passed value when passed an int, so thats the value of c.

If your code was

        int c = Convert.ToInt32(Console.ReadLine());

Then it would work as expected, as Console.ReadLine() returns a string. You could also do:

        int c = Convert.ToInt32(((char)Console.Read()).ToString());

Note that I am casting the 52 returned to a char which would then actually be parsed after calling ToString on it.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1
Console.Read();

gets the next character user inputs. You have used

Convert.ToInt32(Console.Read());

This returns the ASCII value of the number(taken as a character input).For '0' it's 48,for '1' it's 49.this explains why your code always goes into else if. Instead use this :

int val = (int)Char.GetNumericValue(Console.Read())

This val shall store the accurate int value.It's mentioned here

Community
  • 1
  • 1
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25