My input gets checked by "TryParse". And if it is false I want to let the user retype it correctly. But when I click any key after the check it deletes everything above cause of "Console.Clear();". - I want to delete only the input part and nothing above it.
namespace Practice
{
class Program
{
static void Main(string[] args) // Main Method
{
byte age1, age2;
string input;
do
{
Console.Write("Enter Age 1: "); input = Console.ReadLine();
if (!byte.TryParse(input, out age1)) Error();
} while (!byte.TryParse(input, out age1));
do
{
Console.Write("Enter Age 2: "); input = Console.ReadLine();
if (!byte.TryParse(input, out age1)) Error();
} while (!byte.TryParse(input, out age2));
}
static void Error()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Invalid Input...");
Console.ResetColor();
Console.ReadKey();
Console.Clear();
}
}
}
I'm not sure that you can erase a single line from the Console buffer. But you can simulate it if you change the second while in your code:
while (!byte.TryParse(input, out age2) && counter) { Error(); Console.WriteLine("Enter Age 1: {0}",age1); Console.Write("Enter Age 2: "); input = Console.ReadLine(); } // Check And Error Output
with this line
Console.WriteLine("Enter Age 1: {0}",age1);
after theConsole.Clear();
executes, It seems like you have erased the wrong input line.