在 C 中,为什么我有时只需要 getchar() 来删除字符?
In C, why do I only need getchar() to remove characters sometimes?
我正在尝试使用 getchar()
从输入缓冲区中删除字符。在下面的代码中,要求用户输入 select 的选择,然后根据选择,需要另一个输入,类型 int
或类型 char
(字符串)。
在int
的情况下,不需要getcar()
并且scanf
正确地接收输入。但是在 char
的情况下,如果不事先使用 getchar()
,scanf
将无法获得输入。这是有原因的吗?
printf("Available Ciphers:\n1) Caesar Cipher\n2) Vigenere Cipher\nSelected Cipher: ");
if(scanf("%d", &choice) != 1){
printf("Error: Bad selection!\n");
exit(EXIT_SUCCESS);
} else if (choice != 1 && choice != 2){
printf("Error: Bad Selection!\n");
exit(EXIT_SUCCESS);
//If the choice entered is correct, then run the following.
} else {
if(choice == 1){
printf("Input key as nuumber: ");
if(scanf("%d", &caesarkey) != 1){ //Why is getchar() not needed here?
printf("Error: Bad Key!\n");
exit(EXIT_SUCCESS);
}
//morecode here
} else if (choice == 2){
printf("Input key as string: ");
while(getchar() != '\n'); //Why is this needed here?
/*Uses scanf and not fgets, since we do not want the
key to contain the newline character '\n'. This is
due to the fact that the newline character is not
considered in the function that encrypts and decrypts
plaintext and ciphertext.*/
if(scanf("%[^\n]s", vigencipherkey) != 1){
printf("Error, Cannot read inputted key!\n");
exit(EXIT_SUCCESS);
}
//More code here..
}
}
为了读取用户输入的行的剩余部分,可以使用这个函数:
int flush_line(void) {
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
return c;
}
备注:
c
必须定义为 int
以适应类型 unsigned char
的所有值和特殊负值 EOF
.
- 你应该测试
'\n'
和 EOF
否则你会在没有尾随换行符的情况下在文件过早结束时出现无限循环,例如如果您从空文件重定向程序的输入,就会发生这种情况。
- 您可以通过将
flush_line()
的 return 值与 EOF
进行比较来测试文件结尾。
您似乎在扫描字符串而不是 int,因此,您传递的是 int 而不是 int 的地址。
更改此行
if(scanf("%[^\n]s", vigencipherkey) != 1){
到
if (scanf("%d", &vigencipherkey) != 1) {
我正在尝试使用 getchar()
从输入缓冲区中删除字符。在下面的代码中,要求用户输入 select 的选择,然后根据选择,需要另一个输入,类型 int
或类型 char
(字符串)。
在int
的情况下,不需要getcar()
并且scanf
正确地接收输入。但是在 char
的情况下,如果不事先使用 getchar()
,scanf
将无法获得输入。这是有原因的吗?
printf("Available Ciphers:\n1) Caesar Cipher\n2) Vigenere Cipher\nSelected Cipher: ");
if(scanf("%d", &choice) != 1){
printf("Error: Bad selection!\n");
exit(EXIT_SUCCESS);
} else if (choice != 1 && choice != 2){
printf("Error: Bad Selection!\n");
exit(EXIT_SUCCESS);
//If the choice entered is correct, then run the following.
} else {
if(choice == 1){
printf("Input key as nuumber: ");
if(scanf("%d", &caesarkey) != 1){ //Why is getchar() not needed here?
printf("Error: Bad Key!\n");
exit(EXIT_SUCCESS);
}
//morecode here
} else if (choice == 2){
printf("Input key as string: ");
while(getchar() != '\n'); //Why is this needed here?
/*Uses scanf and not fgets, since we do not want the
key to contain the newline character '\n'. This is
due to the fact that the newline character is not
considered in the function that encrypts and decrypts
plaintext and ciphertext.*/
if(scanf("%[^\n]s", vigencipherkey) != 1){
printf("Error, Cannot read inputted key!\n");
exit(EXIT_SUCCESS);
}
//More code here..
}
}
为了读取用户输入的行的剩余部分,可以使用这个函数:
int flush_line(void) {
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
return c;
}
备注:
c
必须定义为int
以适应类型unsigned char
的所有值和特殊负值EOF
.- 你应该测试
'\n'
和EOF
否则你会在没有尾随换行符的情况下在文件过早结束时出现无限循环,例如如果您从空文件重定向程序的输入,就会发生这种情况。 - 您可以通过将
flush_line()
的 return 值与EOF
进行比较来测试文件结尾。
您似乎在扫描字符串而不是 int,因此,您传递的是 int 而不是 int 的地址。
更改此行
if(scanf("%[^\n]s", vigencipherkey) != 1){
到
if (scanf("%d", &vigencipherkey) != 1) {