0

I want to know is there some function similar to scanf that allows custom typing in c. What I'm trying to make is while user is typing wanted date , dot is added every few characters. When user wants to confirm his input , he should press enter, and that variable is saved somewhere.

Example : user is typing 11.04.2015.

When he typed 11 , dot is added on console. Same goes after 4 , and 2015.

Thank you so much for your answers. The platform is Windows

tijetof
  • 51
  • 7
  • 1
    You need to catch stdin and display what you want take a look here http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – Ôrel Apr 11 '15 at 15:08
  • 4
    How will an automatic dot work? If user is typing a date, how is program expected to know whether a `1` will be January or another digit typed as he wants `11` for November? Your example used a single `4` and there is no 2-digit day *or* month (depending on date sequence format) starting with a `4` so it is a poor example. – Weather Vane Apr 11 '15 at 15:17
  • @WeatherVane You are right , I should change that. I keep date inside string (not best idea , but I just play around with code anyway). I though , after user writes 2 characters for day , characters is printed on screen and also is added inside buffer. Same would go for month . For year, dot would appear after 4 characters . – tijetof Apr 11 '15 at 15:54
  • You might want to use [readline](http://www.gnu.org/software/readline/) or [ncurses](http://www.gnu.org/software/ncurses/) - at least on Linux. – Basile Starynkevitch Apr 17 '15 at 12:27

2 Answers2

2

A quick solution, this handle date on format group of 3 block. This add a . after two int

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char ch;
    char input[16];
    char *p;
    int i = 0;
    int part = 0;
    memset(input, 0, sizeof(input));

    p = input;
    system("stty raw -echo");
    while(1)
    {
        ch = getchar();
        if(ch == '~'){//terminate or come out of raw mode on "~" pressed
            system("stty cooked sane");
            return 0;
        }
        if (ch >= '0' && ch <= '9') {
            *p++ = ch;
            if (i++ == 1) {
                if (part++ == 2) {
                    printf("\rdone: %s\n", input);
                    /* Finish your prog here*/
                    break;
                }
                *p++ = '.';
                i = 0;
            }
        } else if (ch == '.') {
            *p++ = ch;
            i = 0;
            part++;
        }
        printf("\r%s", input);
    }
    system("stty cooked sane");
    return 0;
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • You have to add more intelligence to detect one digit month and day – Ôrel Apr 11 '15 at 15:37
  • 1
    `getchar()` does not return a value keystroke by keystroke, so you are unable to reply with the `.` prompts OP asked for. And why is the code sprinkled with incompatible system calls? What is `bzero`? What is `strings.h`? What is wrong with `char input[16] = {0};`? – Weather Vane Apr 11 '15 at 16:13
  • @WeatherVane you need to fill all input with `\0` or add one after each char you add. You can use `memset` instead of `bzero` and remove strings.h I don't get the problem with `getchar` and `.`. Answer updated with `memset` – Ôrel Apr 11 '15 at 16:37
  • First point `char input[16] = {0};` initialises the whole array to `0` without help from a non-standard library. Second point `getchar()` doesn't return until you press `` so you can't anticipate the digit sequence char-by-char and insert a `'.'` on the fly as OP seems to want. So you might as well input each field separately, prompting with the result so far with a trailing `'.'` without any fancy stuff. – Weather Vane Apr 11 '15 at 16:44
  • for me this works (have you try my code ?), when you set stty to raw mode you read without waiting enter press – Ôrel Apr 11 '15 at 16:50
  • Yes of course I tried it before commenting, after removing the system and library specific stuff that I don't have in Windows. Nothing happens until I press ``. The other stuff is OS specific. It's a grand shame that standard C library functions do not return the value of a single keystroke, so MSVC `conio` functions `kbhit()` and `getch()` are OS specific. – Weather Vane Apr 11 '15 at 16:51
  • Ok my bad, I only test on debian and it does the job. `ncurses` or `conio` can be use to deal with the need but it is not POSIX – Ôrel Apr 11 '15 at 17:07
0

Intentional off-topic advice here: don't get in user's way. Implement a function that parses anything looking like date instead, but please, never auto-put dots nor otherwise auto-separate d/m/y fields. If you do any of that, user will be unable to copy-paste dates and enter shortcuts (e.g. 1.1.15) and in general will be angry of such incompetent ui designer. Second, doing it right (what about backspaces? Non-digits? Comma?Dots?) leads to completely idiotic code complexity. Unless you have that as a strict requirement, you're just spending your time annoying your clients.

If you HAVE TO, sorry for the noise!

user3125367
  • 2,920
  • 1
  • 17
  • 17
  • This whole thing was curiosity.I just only wanted to see if this is possible inside Windows , because I started this as side project for practicing programming. This is works inside console , so I don't expect from user to have option for copy-pasting. (what about backspaces? Non-digits? Comma?Dots?) I had made separate function for checking user input , if it is correct. I only allowed user to enter dots. – tijetof Apr 24 '15 at 08:10