• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C/C++. Backspace not working properly

c0rrupt

Weaksauce
Joined
Jun 25, 2004
Messages
102
I can't get the backspace key to work properly. When typing the name it appears to be fine, but once I hit enter and it is printed out it doesn't look the way it should.

Ex.
"Davie[BS]d" will produce Davi once I hit enter

Code:
#include <stdio.h>
#include <conio.h>

char name[32];

void main()
{
  int i = 0;
  while (name[i - 1] != 13)                   
  {
    name[i] = getch();

	//Check for Backspace.
	if (name[i] == 8) 
	{
	        i--;					
		printf("\b%c",0,8);		 
		name[i] = 0;.
	}
	else { printf("%c%c", name[i]); }

    i++;
  }
  name[i] = 0;
  printf("\nThank you %s", name);
  getch();
}
 
I would look at the last line of your IF statment as the problem.
 
Not really sure what you trying to do, but your else statement prints out two characters and doesnt supply an argument for the second character. (Surprised me that compiled and worked, prints out garbage.)

Edit: Oh I get it duh, alright your incrementing i even when backspace is detected, skipping over the null terminator you manually placed over the deleted character.
 
Back
Top