-3

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();
        }
    }
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Are you asking about your code style? – Toumash Dec 04 '14 at 21:15
  • `byte age1, age2` should that be `int || Int32` also please fix the poorly formatted code.. also do not put multiple lines of code that do separate things on a single line.. makes it hard to read what you are trying to do and can easily be looked over or missed also on this line `Console.WriteLine("Age 1-> {0}` it would be cleaning in my opinion if you would use `string.Format()` method – MethodMan Dec 04 '14 at 21:20
  • There's no reason for byte to be changed...byte its 0-255 and I don't think many people live to be close to that age. – Joe Swindell Dec 04 '14 at 21:23
  • I'm not sure that you can erase a single line from the Console buffer. – pepOS Dec 04 '14 at 21:40

1 Answers1

0

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 the Console.Clear(); executes, It seems like you have erased the wrong input line.

pepOS
  • 335
  • 4
  • 22
  • I appreciate it man, I'm thanking you for your response. You helped me. - I apologise if you couldn't understand my question or couldn't read my code. Kinda silly complains from experienced programmers but whatever. - I'm learning C# in the institute for 1 year so far. And I thought it would be handy to learn such presentation feature where invalid input gets removed and replaced by a new string, without clearing everything above. -> If you're curious you can see what I've done-> http://pastebin.com/4v58razK – Dmitrijs Savostijanovs Dec 07 '14 at 11:46
  • @monstrasitix: Glad I helped you. If you can please accept my answer. I saw your code in pastebin. If your age variables keeps growing you should encapsulate the age checking and printing in a method. making your code easier to read and follow when errors appear. – pepOS Dec 08 '14 at 13:38