0
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)

6
  • 11
    What's A here ? Commented Aug 30, 2018 at 17:49
  • You should be able to do that using Console.Write() Commented Aug 30, 2018 at 17:53
  • I assume this is a console app. There is an issue with what you are doing. you can use readkey() instead of read() and store the input into a property. And detect when the "[Enter]" key is pressed, then show the ":" and start capturing 2nd number. Commented Aug 30, 2018 at 17:54
  • 1
    Making some assumptions about how Read() and Write() work, this looks like it'd be confusing to the user since you're initially prompting for both the hours and minutes, yet your code seems to be expecting it be entered as <Hours><Enter><Minutes><Enter>. I would expect your first call to Read() to fail when the user inputs <Hours>:<Minutes><Enter>, even though that's what your asking them to type. I think you'd be better off reading the hours and minutes as one String and just parsing that into its component values. Commented Aug 30, 2018 at 17:59
  • @sean - using readkey() instead of read() is probably not a good idea, as a user would expect handling for backspace etc Commented Aug 30, 2018 at 18:11

3 Answers 3

3

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);

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is so helpful :]
1

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();
    }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.