2

I want a "slash" to appear automatically in my hospital management system. As the user inputs their birthdate using my application, the / delimiters should appear automatically as they type.

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    char slash;
    int dy, mn, yr;

    cout << "\nEnter your date of birth (dd/mm/yyyy) : ";
    cin >> dy;
    cin >> slash;
    cin >> mn;
    cin >> slash;
    cin >> yr;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • It's better to read the entire input into a string and then "parse" the string to find it's components. – Anon Mail Apr 28 '17 at 13:24
  • 1
    i want slash to appear automatically in my program for hospital management system, i have created a class date and person, i have inherited class date to person so that it will take its birthdate. The main point of using this class is when userinputs his birthdate using my application in section birthdate, while filling the dd "/" should appear automatically. what do you mean by research? – Navruzbek Noraliev Apr 28 '17 at 13:36
  • I mean, you should do research before asking us to help. Read things, try things. – Lightness Races in Orbit Apr 28 '17 at 14:00

1 Answers1

3

Assuming you want to output "/" after the user types each two characters, like a kind of "template", that's going to be impossible in the general case.

Terminal emulators tend to be line buffered unless the user changes that setting, and you have no way of controlling that. The line buffering means nothing will be sent to your program unless and until the user hits Enter, which right away ruins the effect:

Animated screenshot of this approach not working

The / was written automatically, but it doesn't look right.

Here's what it might look like if you have full control over the terminal and set it up properly:

Animated screenshot of this approach working

And my PuTTY config at the time:

PuTTY configuration to make the approach work

However, I also had to read the data character by character (otherwise C++ doesn't know you're done after two/four digits), ready for conversion to numerics after-the-fact. Yuck!

The code for the above was as follows:

// Requires "stty raw" to disable line buffering
// ("stty cooked" to restore), as well as no line
// buffering on the client end

#include <iostream>
using std::cin;
using std::cout;
using std::flush;

int main()
{
    char dy1, dy2;
    cout << "\nEnter your date of birth (dd/mm/yyyy) : " << flush;

    cin >> dy1 >> dy2;
    cout << '/' << flush;

    char mn1, mn2;
    cin >> mn1 >> mn2;
    cout << '/' << flush;

    char yr1, yr2, yr3, yr4;
    cin >> yr1 >> yr2 >> yr3 >> yr4;

    std::cout << "\n" << dy1 << dy2
              << '/'  << mn1 << mn2
              << '/'  << yr1 << yr2 << yr3 << yr4 << '\n';

    // Now create some useful integers from these characters
}

Anyway, to really get this right, you'd need to use a "GUI" library like ncurses that can fully take control of the session for you.

Ultimately, it's not worth it. Just let the user enter the full dd/mm/yyyy then parse it for validity.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055