0

I am trying to make a C++ program that accepts a username and password from the user and matches it to the ones that are already stored in the variables. For the password, I am using the * (asterisk) character to appear instead of the actual characters for privacy.

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

void main(void) {
    clrscr();

    gotoxy(10,10);
    cout << "Username: ";
    gotoxy(10,12);
    cout << "Password: ";

    char name1[10] = {"Apple"};
    char name2[10];
    char pass1[10] = {"Orange"};
    char pass2[10] = {""};

    gotoxy(23,10);
    cin >> name2;
    gotoxy(23,12);
    cout << pass2;

    int i = 0;
    char ch;

    while ((ch = getch()) != '\r') {
        putch('*');
        pass2[i] = ch;
        i++;
    }

    if (strcmp(name1, name2) == 0 && strcmp(pass1, pass2) == 0) {
        clrscr();
        gotoxy(40,10);
        cout << "YES!!!";
    } else {
        clrscr();
        gotoxy(40,10);
        cout << "NO!!!";
    }
}

The problem is when I try to use the backspace key on the keyboard, I doesn't delete the character, instead it adds more characters to the end of it. I can make it work by importing the C language's string.h library. But is there any way I can make it work by using the libraries that are already defined in the code without having to use the string.h library.

  • Backspace is a character...handle it accordingly – Grantly Mar 16 '18 at 20:21
  • 2
    If you're trying to take a password as input, the shell usually does it by silencing your input (hiding it). If you're using windows, see [these](https://stackoverflow.com/questions/6899025/hide-user-input-on-password-prompt) [questions](https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin) – scohe001 Mar 16 '18 at 20:23
  • Handle it inside your while loop: `if (ch == '\010' && i) { cout << "\010 \010"; --i; } else { ...existing stuff for other chars... }` – Tony Delroy Mar 16 '18 at 20:33
  • BTW, you can refer to a backspace as '\b'. – Thomas Matthews Mar 16 '18 at 20:50

2 Answers2

1

The problem is when I try to use the backspace key on the keyboard, I doesn't delete the character, instead it adds more characters to the end of it

Correct! When you use the backspace key, it actually generates a backspace character (ASCII 0x8). The backspace key doesn't "delete characters"; you only think it does, because software is normally written to look out for backspace characters and behave specially when it encounters them. In this case, the writer of that software is you!

You'll need to look at the value of ch, see whether it's a backspace character … and, if so, "do the needful" (only you can tell what this is).

I can make it work by importing the C language's string.h library.

Including string.h has nothing to do with it and will have no effect on this.

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

Backspace is considered a "non-printing character", which is where this behavior is coming from. You'll likely have to handle it as you've already thought out in the question.

you can click here for a quick glance at this and other non-printing characters. http://www.physics.udel.edu/~watson/scen103/ascii.html

Lenigod
  • 153
  • 5
  • 17