C# Limit number of characters user can enter - Console Application
-
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
-
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
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
-
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
Thank you so much. This helps a great deal.
-
Thank you so much. This helps a great deal.
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
-
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
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;
} -
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;
}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