0

Im trying to figure out what I am doing wrong. I want the top line of code to ask a question and the if statement to check for string or a number that is not in between 1-12.

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

namespace ConsoleApplication4
{
    class Class1
    {

        public void checknumber()
        {

            string result;
            int input = result;
            Console.WriteLine("Please enter the month in numerical value (1-        12)");
            Console.Write(result);

            if (input < 1 && input > 12)
            {
                Console.WriteLine("Please close the program, run the program     again, and input  number between 1-12");

            }
        }
    }

}

  • What do you mean "check for string or number". You just want to check if it's a number between 1 and 12, right? – pushkin Feb 24 '17 at 04:28
  • 2
    Hi Joseph. Currently this code won't even compile. In terms of what you are doing wrong, are you seeing error messages? Do you have other attempts to show? I think there's a little more you can do to debug this yourself. To give some pointers, you aren't currently reading input from the console and assigning it to anything. That needs to happen with `Console.ReadLine()`. Assign that to `result`. Then, take a look at `int.TryParse`. – Cᴏʀʏ Feb 24 '17 at 04:28
  • You can try IsNumber – A3006 Feb 24 '17 at 04:30
  • You could also use a regular expression. bool isNumber = Regex.IsMatch(result, @"^\d+$") – Chris Feb 24 '17 at 04:38

1 Answers1

1

Try using "Int32.TryParse". It returns false if entered character is not a number.

Console.WriteLine("Please enter the month in numerical value (1-        12)");
if (!Int32.TryParse(Console.ReadLine(), out result))
   {
    result = 0;
   }
    if (result < 1 && result > 12)
     {
       Console.WriteLine("Please close the program, run the program     again, and input  number between 1-12");
      }
Brijesh
  • 369
  • 2
  • 10