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. C# Limit number of characters user can enter - Console Application

C# Limit number of characters user can enter - Console Application

Scheduled Pinned Locked Moved C#
csharplinqalgorithms
6 Posts 3 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.
  • D Offline
    D Offline
    Deborah Palmer McCain
    wrote on last edited by
    #1

    I suspect my mentor is attempting to reduce me to girlie tears. I am to take my basic multiplication code and restrict the number of characters a user can enter (in order to avoid an overload) After searching stackoverflow and Google, I can only find tutorials for textboxes but not working with console applications...or am I simply dim (stop laughing).

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

    namespace debprojectC
    {
    class Program
    {
    static void Main(string[] args)
    {
    int number1, number2;

            Console.WriteLine("Please enter a number:");
    
    
            number1 = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Please enter another number:");
    
            number2 = Int32.Parse(Console.ReadLine());
    
            int result;
            result = number1 \* number2;
            Console.WriteLine("Multiplication:" + result.ToString());
    
            Console.ReadKey();
    
        }
    }
    

    }

    Assistance please. Deborah

    W B 2 Replies Last reply
    0
    • D Deborah Palmer McCain

      I suspect my mentor is attempting to reduce me to girlie tears. I am to take my basic multiplication code and restrict the number of characters a user can enter (in order to avoid an overload) After searching stackoverflow and Google, I can only find tutorials for textboxes but not working with console applications...or am I simply dim (stop laughing).

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

      namespace debprojectC
      {
      class Program
      {
      static void Main(string[] args)
      {
      int number1, number2;

              Console.WriteLine("Please enter a number:");
      
      
              number1 = Int32.Parse(Console.ReadLine());
              Console.WriteLine("Please enter another number:");
      
              number2 = Int32.Parse(Console.ReadLine());
      
              int result;
              result = number1 \* number2;
              Console.WriteLine("Multiplication:" + result.ToString());
      
              Console.ReadKey();
      
          }
      }
      

      }

      Assistance please. Deborah

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      Instead of using Console.ReadLine you are going to have to use Console.ReadKey. Something along the lines of this:-

      string str = string.Empty;
      while (str.Length <= 5)
      {
      char c = Console.ReadKey(true).KeyChar;
      if (c == '\r')
      {
      //user pressed enter
      break;
      }
      if (c == '\b')
      {
      //User pressed backspace
      if (str != string.Empty)
      {
      str = str.Substring(0, str.Length - 1);
      Console.Write("\b \b");
      }
      }
      int enteredValue;
      if(Int32.TryParse(c.ToString(), out enteredValue))
      {
      Console.Write(c);
      str += c;
      }
      }
      Console.WriteLine();
      Console.WriteLine("You entered " + str);
      Console.ReadLine();

      This checks if the char entered is numeric, it handles backspaces and it checks to see if the user pressed enter. I am sure you could alter this to work with your app. Hope it helps

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      D 1 Reply Last reply
      0
      • W Wayne Gaylard

        Instead of using Console.ReadLine you are going to have to use Console.ReadKey. Something along the lines of this:-

        string str = string.Empty;
        while (str.Length <= 5)
        {
        char c = Console.ReadKey(true).KeyChar;
        if (c == '\r')
        {
        //user pressed enter
        break;
        }
        if (c == '\b')
        {
        //User pressed backspace
        if (str != string.Empty)
        {
        str = str.Substring(0, str.Length - 1);
        Console.Write("\b \b");
        }
        }
        int enteredValue;
        if(Int32.TryParse(c.ToString(), out enteredValue))
        {
        Console.Write(c);
        str += c;
        }
        }
        Console.WriteLine();
        Console.WriteLine("You entered " + str);
        Console.ReadLine();

        This checks if the char entered is numeric, it handles backspaces and it checks to see if the user pressed enter. I am sure you could alter this to work with your app. Hope it helps

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        D Offline
        D Offline
        Deborah Palmer McCain
        wrote on last edited by
        #3

        Thank you so much. This helps a great deal.

        W 1 Reply Last reply
        0
        • D Deborah Palmer McCain

          Thank you so much. This helps a great deal.

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #4

          Glad to help :thumbsup:

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          1 Reply Last reply
          0
          • D Deborah Palmer McCain

            I suspect my mentor is attempting to reduce me to girlie tears. I am to take my basic multiplication code and restrict the number of characters a user can enter (in order to avoid an overload) After searching stackoverflow and Google, I can only find tutorials for textboxes but not working with console applications...or am I simply dim (stop laughing).

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

            namespace debprojectC
            {
            class Program
            {
            static void Main(string[] args)
            {
            int number1, number2;

                    Console.WriteLine("Please enter a number:");
            
            
                    number1 = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter another number:");
            
                    number2 = Int32.Parse(Console.ReadLine());
            
                    int result;
                    result = number1 \* number2;
                    Console.WriteLine("Multiplication:" + result.ToString());
            
                    Console.ReadKey();
            
                }
            }
            

            }

            Assistance please. Deborah

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            An alternative way to avoid the overload is just to check that the parsing actually succeeded:

            bool MultiplyFromConsole(){
            int number1, number2;
            string line1, line2;

            Console.Write("Please enter a number: ");
            line1 = Console.ReadLine();

            Console.Write("Please enter a number: ");
            line2 = Console.ReadLine();

            if(!(int.TryParse(line1, out number1) && int.TryParse(line2, out number2))
            return false;

            Console.WriteLine("Multiplication: " + (number1 * number2));
            return true;
            }

            D 1 Reply Last reply
            0
            • B BobJanova

              An alternative way to avoid the overload is just to check that the parsing actually succeeded:

              bool MultiplyFromConsole(){
              int number1, number2;
              string line1, line2;

              Console.Write("Please enter a number: ");
              line1 = Console.ReadLine();

              Console.Write("Please enter a number: ");
              line2 = Console.ReadLine();

              if(!(int.TryParse(line1, out number1) && int.TryParse(line2, out number2))
              return false;

              Console.WriteLine("Multiplication: " + (number1 * number2));
              return true;
              }

              D Offline
              D Offline
              Deborah Palmer McCain
              wrote on last edited by
              #6

              Thank you. My mentor wants me to allow the user another chance if he/she enters a number that is too large or enters letters instead of number. This is quite a learning experience. Thank you for your help. Deborah

              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