[英]How does getchar() work in a While loop?
當我在練習本中工作時,程序的這一部分使我感到困惑。
為什么在交換最后一個if
語句和while getchar()
時得到相同的結果?
在兩種情況下,我都會得到"Enter the next title."
的句子"Enter the next title."
第一。 在兩種情況下,我都會第二次獲取getchar()
,並等待光標閃爍的輸入。
不應該把while(getchar()!= '\\n'); continue;
while(getchar()!= '\\n'); continue;
首先使程序等待輸入,然后再打印puts("Enter the next title.");
?
我的理解是,它應該停留在while循環中,直到存在退出條件為止,然后繼續執行下一條語句! 這是印刷品
這里首先是if語句:
while(count< MAXBKS && s_gets(library[count].title,MAXTITL) != NULL && library[count].title[0] != '\0')
{
puts("Now enter the author.");
s_gets(library[count].author,MAXAUTL);
puts("Now enter the value.");
scanf("%f", &library[count++].value);
if( count < MAXBKS) // 1 //
puts("Enter the next title.");
while(getchar()!= '\n') // 2 //
continue;
}
這是while(getchar()..
while(count< MAXBKS && s_gets(library[count].title,MAXTITL) != NULL && library[count].title[0] != '\0')
{
puts("Now enter the author.");
s_gets(library[count].author,MAXAUTL);
puts("Now enter the value.");
scanf("%f", &library[count++].value);
while(getchar()!= '\n') // 2 //
continue;
if( count < MAXBKS) // 1 //
puts("Enter the next title.");
}
這是上下文的整個程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
char * s_gets(char * st, int n);
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count = 0;
int index, filecount;
FILE *pbooks;
int size = sizeof(struct book);
if ((pbooks = fopen("book.dat", "a+b")) == NULL)
{
fputs("Can't open book.dat file\n", stderr);
exit(1);
}
rewind(pbooks);
while(count< MAXBKS && fread(&library[count],size,1, pbooks) == 1)
{
if (count ==0)
puts("Current contents of book.dat:");
printf("%s by %s: $%.2f\n", library[count].title, library[count].author, library[count].value);
count++;
}
filecount = count;
if(count == MAXBKS)
{
fputs("The book.dat file is full.\n", stderr);
exit(2);
}
puts("Please add new book titles.");
puts("Press [enter] at the start of a line to stop.\n");
while(count< MAXBKS && s_gets(library[count].title,MAXTITL) != NULL && library[count].title[0] != '\0')
{
puts("Now enter the author.");
s_gets(library[count].author,MAXAUTL);
puts("Now enter the value.");
scanf("%f", &library[count++].value);
while(getchar()!= '\n')
continue;
if( count < MAXBKS)
puts("Enter the next title.");
}
if(count>0)
{
puts("Here is a list of your books:");
for(index =0; index < count; index++)
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
fwrite(&library[filecount],size,1,pbooks);
}
else
puts("No books? Too bad.\n");
puts("Bye.\n");
fclose(pbooks);
return(0);
}
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;
ret_val= fgets(st,n,stdin);
if (ret_val)
{
find = strchr(st, '\n');
if(find)
*find = '\0';
else
while(getchar() != '\n')
continue;
}
return (ret_val);
}
不應該把
while(getchar()!= '\\n'); continue;
while(getchar()!= '\\n'); continue;
首先使程序等待輸入,然后再打印puts("Enter the next title.");
?
不, while(getchar()!= '\\n'); continue;
while(getchar()!= '\\n'); continue;
將清除輸入緩沖區1 ,它將不等待用戶輸入。 然后它將打印文本。 它不等待用戶輸入內容的原因是您之前有一個scanf
。 如果它與浮點數匹配,它將對其進行轉換並保存在&library[count++].value
,但是換行符將保留在輸入緩沖區中。 這就是為什么人們使用這種方法清除輸入緩沖區中的其余部分。 如果不匹配,則整行將保留在輸入緩沖區中。
因此,先執行哪個都無關緊要,緩沖區將被清除並且文本將被打印。 下一個s_gets()
調用中的fgets
阻止並等待用戶輸入,而不是getchar()
。
我的理解是,它應該停留在while循環中,直到存在退出條件為止,然后繼續執行下一條語句! 這是印刷品
因此,您很可能會誤解了誰阻止並等待用戶輸入。
一些建議:
清除緩沖區最好像這樣完成:
int c;
while( (c = getchar()) != '\n' && c != EOF);
此外,最好始終檢查scanf
的返回值。 它將返回成功匹配令牌的數量。 因此,如果您希望進行一次轉換,請檢查scanf
返回1。如果不是這種情況,則輸入錯誤。 您可以決定清除緩沖區,然后讓用戶再次輸入重試。
1顯然,如果第一個讀取代碼是while(getchar() != '\\n') continue;
那么它將阻塞並等待用戶輸入,因為輸入緩沖區將為空。 但是在您的情況下,輸入緩沖區肯定不是空的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.