0
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char pwd[5];
int i;
printf("Enter your password: ");
for(i=0;i<=3;i++)
{
pwd[i]=_getch();
printf("%c",pwd[i]);
 if(pwd[i]==8)
  {
    if(i>0)
    {
    printf(" \b");
    i--;
    continue;
    }
    else
        break;
  }
}
pwd[i]='\0';
if(strcmp(pwd,"abcd")==0)
{
printf("\nPassword is correct");
}
else
{
printf("\nPassword is not correct");
}
return 0;
}

I want output in such form that when the user press backspace the previous character should be deleted and the user should be allowed to re-enter the previous character but this code has some problems in it. It deletes the previous character but doesn't allow to re-enter previous corrected character instead it takes it as next character what is the problem with this code please explain?

  • One problem is using archaic C compilers that support `dos.h` and `conio.h` — you should upgrade to something from the current millennium, rather than use some that was prehistoric at the turn of the millennium. – Jonathan Leffler Jun 22 '21 at 17:26
  • 1
    Your code does nothing special with the backslash character on input. You detect the backspace character (badly — you should use `'\b'` instead of 8), but that's very different from the backslash character. – Jonathan Leffler Jun 22 '21 at 17:29
  • 2
    Please explain more what you mean by "when I input something it misbehaves". What is "something"? What kind of misbehavior are you encountering? – Raymond Chen Jun 22 '21 at 17:46
  • Please [edit] to explain. – Yunnosch Jun 22 '21 at 17:56
  • Aside: please place the newline at the end of the message, not the beginning, for example `printf("Password is correct\n");` – Weather Vane Jun 22 '21 at 21:52
  • I request you to read the edited question. – pratik jain Jun 23 '21 at 05:39

1 Answers1

1

There is a bug in this code. At the 18th line inside the if (i > 0) statement, you are reducing the value of variable i by 1, which is alright. But then, you are not "reinputting" pwd[i]. And remember that adding '\b' to your string doesn't remove the previous character of it. It rather "adds" a new character, but you were outsmarted by the output. So here is a working code with the bug fixed:

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    char pwd[5];
    int i;
    printf("Enter your password: ");
    for(i=0; i<=3; i++)
    {
        label:
        pwd[i]=_getch();
        printf("%c",pwd[i]);
        if(pwd[i]==8)
        {
            if(i>0)
            {
                printf(" \b");

                i--;
                goto label;
                continue;
            }
            else
                break;
        }
    }
    pwd[i]='\0';
    if(strcmp(pwd,"abcd")==0)
    {
        printf("\nPassword is correct");
    }
    else
    {
        printf("\nPassword is not correct");
    }
    return 0;
}

I think this is enough explanation from me. The rest is for you to take your time and understand the code. Happy coding!

  • Thank you but please tell me if this code can be written without goto because my teacher said that you should not use goto as it is a bad programming practice. – pratik jain Jun 23 '21 at 08:49
  • 1
    "goto isn't bad itself. It is bad only when they're used in inappropriate places." Many beginners make mistakes when using goto because of a lack of understanding in some points. If you want to read a complete argument on whether goto is good or bad, [here is a post](https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto) on stackoverflow. Short answer: there is no bad thing in using goto at appropriate places(such as jumping out of a loop). If you are still concerned, then comment below, I'll find some other way. – Programmer Hobbyist Jun 23 '21 at 09:04
  • 1
    I have doubt in this part of the code:-. for(i=0;i<=3;i++) { pwd[i]=_getch(); printf("%c",pwd[i]); if(pwd[i]==8) { if(i>0) { printf(" \b"); i--; continue; } else break; } }. Why the continue statement after decrementing the value of i by 1 doesn't go and again to the top of the loop and take value of in the decremented pwd[i] vai _getch() again and overwrite previous value with it please explain where I am wrong? Why I need to add goto in it. – pratik jain Jun 23 '21 at 09:56
  • 1
    I understand what you mean. But you must have forgotten that in the `for(i=0; i<=3; i++)` statement, the value of variable `i` is again increased by 1. The `continue;` statement skips the next part of the loop, but also takes the program to the next round of it. And as you already know, the `update statement` of a loop, which is `i++` in this case, executes at each round of a for-loop. But you don't want the variable `i` to be decremented before the input is taken again. That is why you need goto, so that you can take the input again before the loop goes to its next round and increases `i`. – Programmer Hobbyist Jun 23 '21 at 10:17
  • Glad to help you out! And here is an extra tip: Debugging is helpful in most cases to shoot the bug. You can [read this article](https://en.wikipedia.org/wiki/Debugging) or google to learn about debugging. – Programmer Hobbyist Jun 23 '21 at 10:59