Think of typing a password. You type your password but what you see is merely a asterisk sign. If you use getch(), no problem! Put the problem is that I can't use the getch. See the code:

struct Func { 
    char password[9];
    char name[34];
    char code[8];
}element;

do{
    gotoxy(21,6);
    printf("****************************************");
    gotoxy(21,7);
    printf("**Password:                                          *******");
    gotoxy(21,8);
    printf("****************************************");
    gotoxy(46,7);
    cin>>element.password;  
}while(strlen(element.password)<4||strlen(element.password)>8);
fich.write((char *)&element,sizeof(Func));
fich.close();

Can some please tell how can i hide the password, in this case? :cry:

Dani AI

Generated

— the reason you can’t get per‑character asterisks with cin >> is that the terminal is doing the echoing and cin reads buffered input. is correct about buffering, and is right that there’s no single portable C/C++ call: you must either change the terminal mode or use a platform/library routine that reads raw key presses and prints * yourself.

Two practical approaches:

  • Quick and simple (cross‑platform): temporarily turn off console echo and read the whole line. This prevents typed characters from appearing (no stars), then restore the mode. It’s small and safe if you only need concealed input.
  • Full cosmetic behaviour (shows * while typing): read input character‑by‑character with echo disabled and print * for each key. This requires platform-specific code (POSIX: termios; Windows: console APIs or a library). Below is a POSIX example that reads raw keystrokes, prints *, and handles backspace and length limits.
/* POSIX: disable echo + noncanonical so getchar() returns immediately */
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

char *get_masked_password(char *buf, size_t maxlen) {
    struct termios oldt, newt;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ECHO | ICANON);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    size_t len = 0;
    int c;
    while ((c = getchar()) != '\n' && c != '\r' && len < maxlen) {
        if (c == 8 || c == 127) {           /* backspace */
            if (len) { len--; printf("\b \b"); fflush(stdout); }
            continue;
        }
        buf[len++] = (char)c;
        putchar('*'); fflush(stdout);
    }
    buf[len] = '\0';
    putchar('\n');
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    return buf;
}

Notes and cautions:

  • If you’re on Windows and can’t use getch(), either use the WinAPI console modes or a library (ncurses/pdcurses) to get raw key events.
  • Do not store plaintext passwords in files. Hash with a strong algorithm (bcrypt/Argon2/PBKDF2) and store the salted hash instead.
  • Avoid fixed small buffers (your original 9‑byte array limits you to 8 chars plus NUL); prefer std::string or enforce bounds and clear sensitive memory after use.

Recommended Answers

All 2 Replies

There is no way with cin>> because the input is buffered.

>the problem is that I can't use the getch.
Then you have to write a routine with equivalent functionality. There's no standard way to do what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.