x = getchar(); ????
-
Pls examine the code below, provided all the variable is initialized. Now at this point, i prompt for the use to type in one character, and the code will react depends on what key the user pressed. If the key is something outside 0-9, a-z, A-Z, - and _, it beeps. If Enter was press, the loop ended, if back space were press, it goes back one character. But it is not working. Can anyone tell me what's wrong? char Login[12]; int i; for (i=0; i<12; i++) { Login[i] = getchar(); typed = Login[i]; if (typed == 8 && i > 0) { x = x+i-1; printf("backspace pressed"); gotoxy(x,13);cprintf(" "); } if (typed == 13) // I tot 13 was the RETURN key??? { i = 12; printf("RETURN key pressed"); } if ((typed >= 0 && typed <= 12) || (typed >= 14 && typed <= 44)) { sound(1000); delay(500); nosound(); --i; } if ((typed >=46 && typed <= 47) || (typed >= 58 && typed <= 64)) { sound(1000); delay(500); nosound(); printf("second"); --i; } if ((typed >= 91 && typed <= 96) || (typed >= 123 && typed <= 255)) { sound(1000); delay(500); nosound(); --i; } }
-
Pls examine the code below, provided all the variable is initialized. Now at this point, i prompt for the use to type in one character, and the code will react depends on what key the user pressed. If the key is something outside 0-9, a-z, A-Z, - and _, it beeps. If Enter was press, the loop ended, if back space were press, it goes back one character. But it is not working. Can anyone tell me what's wrong? char Login[12]; int i; for (i=0; i<12; i++) { Login[i] = getchar(); typed = Login[i]; if (typed == 8 && i > 0) { x = x+i-1; printf("backspace pressed"); gotoxy(x,13);cprintf(" "); } if (typed == 13) // I tot 13 was the RETURN key??? { i = 12; printf("RETURN key pressed"); } if ((typed >= 0 && typed <= 12) || (typed >= 14 && typed <= 44)) { sound(1000); delay(500); nosound(); --i; } if ((typed >=46 && typed <= 47) || (typed >= 58 && typed <= 64)) { sound(1000); delay(500); nosound(); printf("second"); --i; } if ((typed >= 91 && typed <= 96) || (typed >= 123 && typed <= 255)) { sound(1000); delay(500); nosound(); --i; } }
A few points, forgive me if I'm mistaken: Where's your code to actually print the character... something like: if (typed >= (short)('A') && typed<=(short)('Z')) { gotoxy(x+i,13); cprintf(typed); } also, your backspace code needs to read something like: if (typed == 8 && i > 0) { gotoxy(x+i-1,13);cprintf(" "); i--; } Where 'x' is your initial starting point.
-
A few points, forgive me if I'm mistaken: Where's your code to actually print the character... something like: if (typed >= (short)('A') && typed<=(short)('Z')) { gotoxy(x+i,13); cprintf(typed); } also, your backspace code needs to read something like: if (typed == 8 && i > 0) { gotoxy(x+i-1,13);cprintf(" "); i--; } Where 'x' is your initial starting point.
You are correct in both code you told me. But because i use getchar() function, whatever input from the keyboard will be echoed (show on the screen). So, my main point for this is not about showing use what they type, but is that i want to limit my user to only certain keys. Keys allow: a-z, A-Z, 0-9, - and _, backspace, Enter Everything outsite of that range will falg as an error and the program will beep... usage: backspace key : erase previously typed character enter key : tell the program that the imput is finish. So, get my point? Actually this portion of the program is for use at a login screen, where people use to type int her user name.
-
You are correct in both code you told me. But because i use getchar() function, whatever input from the keyboard will be echoed (show on the screen). So, my main point for this is not about showing use what they type, but is that i want to limit my user to only certain keys. Keys allow: a-z, A-Z, 0-9, - and _, backspace, Enter Everything outsite of that range will falg as an error and the program will beep... usage: backspace key : erase previously typed character enter key : tell the program that the imput is finish. So, get my point? Actually this portion of the program is for use at a login screen, where people use to type int her user name.
you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this
char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);
hope this might help cheers G Alfadhly
-
you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this
char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);
hope this might help cheers G Alfadhly
-
you are using getchar() The getchar() function will wait for a carriage return even if setbuf() is used to unbuffer stdin use getch instead include << conio.h >> to be able to use getch() BTW: I would not assign Login[i] = getch(); typed = Login[i]; I would assign it to typed and test it then assign it to Login[i] if it is ok so you don't have to go back try this
char Login[12]; int i, x; char typed ; for (i=0; i<12; i++) { typed = getch(); if (typed >='1' &&typed <='9' || typed >='A' && typed <='Z' || typed >='a' && typed <='z' || typed == '_') { printf("*"); Login[i] = typed ; }else { i--; printf("\a"); } } printf("%s",Login);
hope this might help cheers G Alfadhly