0

I've been trying to transfer what I learned in a college class about Visual Basic into C#, and I'm having an issue trying to get the else part of my statement to show up instead of the if. Here's what I'm trying to deal with. It's a basic console app, the purpose is to tell a person who enters their age if they are an adult or not.

using System;

namespace CSharpConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

            int iNumber = 0;

            Console.Write("Input your age: ");
            iNumber = Console.Read();

            if (iNumber >= 18)
            {

                Console.Write("You are an adult!");

            }    
            else
            {

                Console.WriteLine("You are not an adult!");

            }    
        

            Console.ReadKey();

       

        }
    }
}

When I try and enter a value below 18, I get stuck with "You are an adult!" instead of "You are not an adult!" I appreciate any help!

  • 3
    Did you debug the code and check whats the value assigned to `iNumber`? – Chetan May 08 '21 at 02:18
  • 1
    You are calling `Console.Read()`, which returns the UTF16 character code for a single character the user inputs. You need to use `Console.ReadLine()` and then `int.Parse()` to convert the user's inputted text into the integer value. See duplicate. – Peter Duniho May 08 '21 at 02:22

0 Answers0