Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. accept only numbers

accept only numbers

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kabutar
    wrote on last edited by
    #1

    i want my code to accept only numbers as the input in this calculator code.....how can i do that class Program { int operand1; int operand2; char oper; int result; public void Display() { Console.WriteLine("Enter the value of operand1"); if (operand1 < 48 && operand2 > 57) { Console.WriteLine(""); } operand1 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the value for operand2"); operand2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the oper"); oper = Convert.ToChar(Console.ReadLine()); } public void Result() { switch (oper) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; } } public void Displayresult() { Console.WriteLine("{0} ", result); Console.ReadLine(); } public static void Main() { Program obj = new Program(); obj.Display(); obj.Result(); obj.Displayresult(); } } } thanks

    C#

    G 1 Reply Last reply
    0
    • K kabutar

      i want my code to accept only numbers as the input in this calculator code.....how can i do that class Program { int operand1; int operand2; char oper; int result; public void Display() { Console.WriteLine("Enter the value of operand1"); if (operand1 < 48 && operand2 > 57) { Console.WriteLine(""); } operand1 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the value for operand2"); operand2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the oper"); oper = Convert.ToChar(Console.ReadLine()); } public void Result() { switch (oper) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; } } public void Displayresult() { Console.WriteLine("{0} ", result); Console.ReadLine(); } public static void Main() { Program obj = new Program(); obj.Display(); obj.Result(); obj.Displayresult(); } } } thanks

      C#

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Use Int.TryParse() in order to check if the string is an integer

      #region signature my articles #endregion

      K 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        Use Int.TryParse() in order to check if the string is an integer

        #region signature my articles #endregion

        K Offline
        K Offline
        kabutar
        wrote on last edited by
        #3

        hi , thanks but can you be bit more specific ie wer should i use this thanks

        C#

        G 1 Reply Last reply
        0
        • K kabutar

          hi , thanks but can you be bit more specific ie wer should i use this thanks

          C#

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          You should use it instead of Int.Parse() Have a look at documentatio in MSDN, it shows how to use Int.TryParse()

          #region signature my articles #endregion

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups