How to remove vector character by pressing Backspace on c

Asked

Viewed 157 times

0

Good evening... I am making an automatic login system and the validation worked agr the only problem is that it is not possible to delete the previous character if the user misses the letter, I would like to know how would the user event press the Backspace key to delete the wrong character Obs - code below

   #include <stdlib.h>
   #include <stdio.h>
   #include <windows.h>
   #include <string.h>
   char vetor[50];
   char user[]= "usuario";
   char senha[]= "senha";
   int i = 0, c, t=0, j=0, cont=0;

   int main(){

       void Validacao(char vetor[50],int valida, char desejado[50]);
       void ValidacaoSenha(char vetor[50],int valida, char desejado[50]);

       //para entrada e reconhecimento da senha versão 1.0
       printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\tLogin >> ");
       Validacao(vetor,strlen(user),user);
   }

   void Validacao(char vetor[50],int valida, char desejado[50]){

       int i = 0, c, t=0, j=0, cont=0;

       do{
           c=getch();
           fflush(stdin);
           vetor[i] = c;

           printf("%c", vetor[i]);
           i++;

               if(vetor[j] == desejado[j]){
                   cont++;
               }
               j++;

           if(cont==valida){
               t=1;
           }
       } while (t!=1);
   }

1 answer

0

First of all, getch() and fflush(stdin) are two of the most harmful things in C; the first is not part of the pattern and the second generates undefined behavior. If the intention is to just read a user string, and then compare it to another string, there is no need to make character by character, do everything with same strings:

do {
    scanf(" %s", vetor);
} while(strcmp(vetor, desejado) != 0);

Note that the reading only happens after the user keystrokes Enter. And that also assumes that you won’t type more than 50 characters at a time.

  • 1

    Even better to say scanf(" %49s", vetor); that guarantor that the scanf() will not read more than 49 characters at once (leaving the 50th free to win the '\0' terminator).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.