Block input of characters in C
renaudh
Posted messages
143
Registration date
Status
Member
Last intervention
-
Vercors -
Vercors -
Bonjour,
Pour bloquer la saisie de caractères non désirés dans un programme C et pour que les mauvais caractères n'apparaissent pas à l'écran, vous pouvez utiliser la fonction `getch()` au lieu de `getche()`. La fonction `getch()` ne montre pas le caractère à l'écran lorsqu'un utilisateur appuie sur une touche. Voici comment vous pouvez modifier votre boucle :
Avec cette modification, le programme ne montrera pas les mauvais caractères saisis et continuera de demander une saisie valide.
Merci pour votre question.
Pour bloquer la saisie de caractères non désirés dans un programme C et pour que les mauvais caractères n'apparaissent pas à l'écran, vous pouvez utiliser la fonction `getch()` au lieu de `getche()`. La fonction `getch()` ne montre pas le caractère à l'écran lorsqu'un utilisateur appuie sur une touche. Voici comment vous pouvez modifier votre boucle :
#include <conio.h> // Inclure la bibliothèque pour getch()
int main() {
char c;
do {
c = getch(); // Lire un caractère sans l'afficher
if ((c >= 49) && (c <= 55)) { // Vérifier si c est entre 1 et 7
// Faire quelque chose avec c
printf("\nVous avez entré: %c\n", c);
} else {
printf("\nEntrée invalide. Essayez encore.\n");
}
} while ((c < 49) || (c > 55)); // Boucle jusqu'à ce qu'une entrée valide soit donnée
return 0;
}</conio.h> Avec cette modification, le programme ne montrera pas les mauvais caractères saisis et continuera de demander une saisie valide.
Merci pour votre question.
13 answers
-
Hello
replacec=getche(); printf("\n");
withwhile((c=getche())<45 ||c>55); printf("%c\n",c);--
best regards
¤ -
In my opinion, we should look into the echo/noecho options of the terminal (like for password input).
-
I tried your blurk treatment but it's actually worse, I can still enter any character and on top of that, it doesn't even take the input anymore.
As for the echo/noecho, are you sure you're talking about the C language because that reminds me of scripts! -
I suggest this modification:
printf("%s, enter the column\n", joueur1);
c=getch();
while ((c<49)||(c>55))
{
c=getch();
}
It should work. -
-
-
```html
To implement a menu that allows only numeric input (1 to 4) and keeps prompting the user until a valid choice is made, you can use the following approach:
int choice; do { printf("Please enter a number between 1 and 4: "); // Check if the input is valid if (scanf("%d", &choice) != 1) { // Clear the input buffer if a letter or invalid character is entered printf("Invalid input. Please enter numbers only.\n"); while (getchar() != '\n'); // clear the buffer choice = 0; // reset choice to continue the loop } } while (choice < 1 || choice > 4);This code will ensure that the user keeps seeing the menu and will not exit until a valid number (between 1 and 4) is entered.
```
-
-
Hello.
Look there https://en.cppreference.com/w/ on C I/O
try scanf("%[1234567]\n",&c);
or scanf("%c\x8",&c);
\x8 is the character for backspace (delete) ASCII code 8.
I guarantee nothing, but it might be an idea.
otherwise, try ungetc
or even:
c=getch();
if (c...) printf("%c",char(8));
--
Greetings! When you don't know, don't touch!JBT
Char Snipeur -
- ASCII code 8 corresponds to the left arrow instead of backspace (127).
- From the moment we use scanf, input validation only occurs when the user presses the enter key.
It is therefore impossible to prevent a character from being typed using this function.
Solutions for this kind of problem are of the type while ! kbhit() or getch(), but that's Borland and not available on Unix. -
No, 127 is the delete (DELETE) that erases the next character.
See here:
https://fr.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange
and here
https://en.cppreference.com/w/cpp/language/ascii
I can't get the thing to work, but it should be doable in C++ with cin
--
Greetings! If you don't know, don't touch!JBT
Char Sniper -
<supr> delete what is to the right of the cursor, it is found in 2 copies on the numeric and positioning keypads, backspace deletes what is to the left of the cursor, and is located above the enter key. It is indeed backspace that we are talking about.
⌂</supr> -
Heu... blurk, that's not what I just said?
So we agree: 8 corresponds to backspace (left arrow) and 127 to delete (del or remove).
To delete a written character, I tested this and it works:
printf("->a\x8%c\x8",char(32));
this line writes "->a" then backspace moves the cursor back one space, char(32) (the space) erases 'a' from the screen, then we place the cursor behind '->' with the command \x8.
That's it, it doesn't solve your problem, but it gives you an idea of what can be done with ASCII codes.
--
Greetings! When you don't know, don't touch!JBT
Char Snipeur -
Hi sniper
Actually, not quite
8 is the left arrow which has no erase value
9 is for the right arrow
Backspace is like 8 but with an erase
Delete is like a 9 with an erase
Also, be careful, this is ASCII, with code pages it doesn't work the same way
ASCII stops at 127 and beyond, each language (or country) has its own system of accented characters. (Below 32 which corresponds to the space bar, the same, it changes depending on the code page)
Under Windows, what is below 32 or above 127 does not behave the same way as in a Unix terminal or Minitel
Basically, it won't behave the same way under Windows and Linux and on a Mac, because ASCII nowadays is covered by a layer of code pages.
Try pressing the alt key, and at the same time you type the ASCII code, and then you see what it does
See you+ -