eliminate line break after read in C#

511 Views Asked by At
using A = System.Console;
public void point()
{
    int hour, minute;
    A.Write("Enter Time (HH:MM) = ");
    hour = A.Read();
    A.Write(":");
    minute = A.Read();
}

I want it to be like

"Enter Time (HH:MM) = 12(hour input):49(minute input)"

but it coming like

"Enter Time (HH:MM) = 12(hour input)
:49(minute input)

3

There are 3 best solutions below

1
subdeveloper On BEST ANSWER

Simplest way (assuming you are reading from Console, and user will enter hour then press Enter, then enter minute and press Enter):

static void Main(string[] args)
        {
            int hour = 0, minute = 0;
            const int MAX_NUMBER_OF_DIGITS = 2 ;

            Console.Write("Enter Time (HH:MM) = ");
            
            // store cursor position
            int cursorLeft = Console.CursorLeft;
            int cursorTop = Console.CursorTop;
            
            // use ReadLine, else you will only get 1 character 
            // i.e. number more than 1 digits will not work
            hour = int.Parse(Console.ReadLine());

            Console.SetCursorPosition(cursorLeft + MAX_NUMBER_OF_DIGITS , cursorTop);

            Console.Write(":");

            minute = int.Parse(Console.ReadLine());

            // Nitpickers! purposefully not using String.Format, 
            // or $, since want to keep it simple!
            Console.Write("You entered: " + hour + ":" + minute);
        }

Output:

Enter Time (HH:MM) = 17:55

You entered: 17:55

Though I would rather recommend you better and less error prone way like this (where user inputs HH:MM and presses Enter a single time i.e. enters a single string including : i.e. colon):

static void Main(string[] args)
{
    int hour = 0, minute = 0;

    Console.Write("Enter Time in format HH:MM = ");

    string enteredNumber = Console.ReadLine();

    string[] aryNumbers = enteredNumber.Split(':');

    if (aryNumbers.Length != 2)
    {
        Console.Write("Invalid time entered!");
    }
    else
    {
        hour = int.Parse(aryNumbers[0]);
        minute = int.Parse(aryNumbers[1]);

        // Nitpickers! purposefully not using String.Format, 
        // or $, since want to keep it simple!
        Console.Write("You entered: " + hour + ":" + minute);

    }
}
0
Steve On

Assuming A is the standard c# Console then you can use ReadKey instead of Read

ReadKey will read only one character at a time but it will not force you to hit enter which is the cause for the new line.

    static void Main()
    {
        char h1, h2, m1, m2;
        Console.Write("Enter Time (HH:MM) = ");
        h1 = Console.ReadKey().KeyChar;
        h2 = Console.ReadKey().KeyChar;
        Console.Write(":");
        m1 = Console.ReadKey().KeyChar;
        m2 = Console.ReadKey().KeyChar;
    }

I will leave the actual value parsing as an exercise.

0
Unknown User On

Assuming that A is Console, you can do like this:

    static void Main()
    {
        int hour, minute;
        char[] hourChars = new char[2];
        Console.Write("Enter Time (HH:MM) = ");
        hourChars[0] = Console.ReadKey().KeyChar;
        hourChars[1] = Console.ReadKey().KeyChar;
        var hourString = new String(hourChars);
        hour = int.Parse(hourString);
        Console.Write(":");
        minute = Console.Read();
    }