5

I want to get date of birth in one line:

#include <stdio.h>

int main()
{
    int BirthYear,BirthMonth,BirthDay;
    printf("Please enter your birth date: ");
    scanf("%d",&BirthYear);
    printf("/");
    scanf("%d",&BirthMonth);
    printf("/");
    scanf("%d",&BirthDay);
    return 0;
}

This is my output:

Please enter your birth date: YYYY
/MM
/DD

But I want to get something like this:

Please enter your birth date: YYYY/MM/DD

In output, it goes to next line after each scanf() without using \n. I use VS Code for IDM.

Parsa Saberi
  • 105
  • 7

5 Answers5

5

Here is a workaround using ansi control characters. I would not do like this, but just to show that it is possible:

#define PREVLINE "\033[F"
#define MSG "Please enter your birth date: "

int main(void) {
    int BirthYear,BirthMonth,BirthDay;
    
    printf(MSG);
    scanf("%d",&BirthYear);
    printf(PREVLINE MSG "%d/", BirthYear);
    scanf("%d",&BirthMonth);
    printf(PREVLINE MSG "%d/%d/", BirthYear, BirthMonth);
    scanf("%d",&BirthDay);
    printf("You entered: %d/%d/%d\n", BirthYear, BirthMonth, BirthDay);
}

Please note that this is not portable. The terminal needs to support this in order to work. AFAIK there's no 100% portable way to achieve this.

If you want to do this stuff for real, then I recommend taking a look at the ncurses library

Note:

Always check the return value for scanf to detect errors.

Note2:

It may be a good idea to add fflush(stdout); after each printf statement.

I actually wrote another answer today about ascii control characters. It might be interesting: https://stackoverflow.com/a/64549313/6699433

klutt
  • 30,332
  • 17
  • 55
  • 95
3

You can explicitly specify that the three input numbers should be separated by a '/' character by adding that character in the format specifier for the scanf function.

Then, you can ensure that the user gave valid input by checking the value returned by scanf (which will be the number of items successfully scanned and assigned); if that value is not 3, then you will (probably) need to clear any 'leftover' characters in the input buffer, using a getchar() loop until a newline (or end-of-file) is found:

#include <stdio.h>

int main()
{
    int BirthYear, BirthMonth, BirthDay;
    int nIns = 0, ch;
    while (nIns != 3) {
        printf("Enter D.O.B. (as YYYY/MM/DD): ");
        nIns = scanf("%d/%d/%d", &BirthYear, &BirthMonth, &BirthDay);
        while ((ch = getchar() != '\n') && (ch != EOF))
            ; // Clear remaining in-buffer on error
    }
    printf("Entered data were: %d %d %d!\n", BirthYear, BirthMonth, BirthDay);
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
2

Expanding on my comment...

The problem you're running into is that you have to hit Enter for each input, which writes a newline to the terminal screen. You can't avoid that.

And unfortunately, you can't overwrite the newline on the screen with a '\b'; you can only backspace up to the beginning of the current line, not to a previous line.

You basically can't do what you want with vanilla C - the language only sees byte streams, it has no concept of a "screen".

There are some terminal control sequences you can play with to reposition the cursor after sending the newline; I don't know how well those will work for you.

Beyond that, you'll need to use a library like ncurses.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1
#include <stdio.h>

int main()
{
    int BirthYear,BirthMonth,BirthDay;
    printf("Please enter your birth date: ");
    scanf("%d/%d/%d",&BirthYear,&BirthMonth,&BirthDay);
    return 0;
}

You can take multiple values from scanf which are then separated by any text you like (in this case /s).

anastaciu
  • 23,467
  • 7
  • 28
  • 53
R Z
  • 422
  • 4
  • 13
-2

You could use fflush(3) (in particular before all calls to scanf(3)) like in

printf("Please enter your birth date: ");
fflush(NULL);

but you should read this C reference website, a good book about C programming such as Modern C, and the documentation of your C compiler (perhaps GCC) and debugger (perhaps GDB).

Consider enabling all warnings and debug info in your compiler. With gcc that means compiling with gcc -Wall -Wextra -g

Be aware that scanf(3) can fail.

Your code and problem is surely operating system specific.

On Linux consider using ncurses (for a terminal interface) or GTK (for a graphical interface). Read also the tty demystified, then Advanced Linux Programming and syscalls(2) and termios(3).

You might also consider using ANSI escape codes, but be aware that in 2020 UTF-8 should be used everywhere.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547