C/C++ getch() function is not working as it should

580 views Asked by At

When attempting to use the getch() function in order to read a keyboard input in my code, it just ends up reading all inputs until the Enter key is pressed. I'm trying to make my own input where it replaces each input with an asterisk.

#include <iostream>
#include <conio.h>

using namespace std;

int get_pass() {
    int pwd[9];

    cout << "Enter Your Password: ";

    for(signed int i = 0; i < 8; i++){
        int pass_input;

        pass_input = getch();

        if (pass_input == 13) { // If the Enter key is pressed
            return 0;
        }

        pwd[i] = pass_input;

        cout << "*";
    }

    cout << endl;
}

int main() {
    get_pass();
    return 0;
}

Result:

Enter Your Password: abc123
****** <- After I press Enter

How can I fix this problem?

0

There are 0 answers