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)

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Jan Nutcha
  • 83
  • 1
  • 1
  • 7
  • 11
    What's `A` here ? – Hatted Rooster Aug 30 '18 at 17:49
  • You should be able to do that using Console.Write() – Eugene Aug 30 '18 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. – sean Aug 30 '18 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 ``. I would expect your first call to `Read()` to fail when the user inputs `:`, 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`](https://stackoverflow.com/q/3881826/150605) and just parsing that into its component values. – Lance U. Matthews Aug 30 '18 at 17:59
  • @sean - using readkey() instead of read() is probably not a good idea, as a user would expect handling for backspace etc – Joe Aug 30 '18 at 18:11
  • Hmmm. Three of the four posted answers, plus mine and @sean's comments, make use of the same word: "assuming". The fourth instead uses "guessing". – Lance U. Matthews Aug 30 '18 at 18:42

3 Answers3

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

    }
}
Community
  • 1
  • 1
subdeveloper
  • 1,381
  • 8
  • 21
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();
    }
Unknown User
  • 78
  • 1
  • 7
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.

Steve
  • 11,696
  • 7
  • 43
  • 81