while 循环没有按预期继续
The while loop doesn't continue as intended
出于某种原因,一旦我输入要搜索的字符,main 中的 while 循环就会终止,但目的是让您能够输入一行,然后输入一个字符进行搜索,直到您输入一个空行(输入没有什么)。基本上我想无限地执行第 1 步和第 2 步,直到我什么都不输入并按回车键。为什么这不起作用?感谢任何人的帮助!
另外,一个小问题,输入字符进行搜索后如何清除垃圾?
#include <stdio.h>
#define SIZE 41
int CharIsAt(char *pStr,char ch,int loc[],int mLoc);
int main(void){
char array[SIZE],search;
int found[SIZE],i,charsFound;
//Step 1
printf("Enter a line of text(empty line to quit): ");
while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n'){ //Loop until nothing is entered
//Step 2
printf("Enter a character to search: ");
search=getchar();
charsFound=CharIsAt(array,search,found,SIZE);
printf("Entered text: ");
fputs(array,stdout);
printf("Character being searched for: %c\n",search);
printf("Character found at %d location(s).\n",charsFound);
for (i=0;i<charsFound;i++)
printf("%c was found at %d\n",search,found[i]);
printf("Enter a line of text(empty line to quit): ");
}
return 0;
}
int CharIsAt(char *pStr,char ch,int loc[],int mLoc){
//Searches for ch in *pStr by incrementing a pointer to access
//and compare each character in *pStr to ch.
int i,x;
for (i=0,x=0;i<mLoc;i++){
if (*(pStr+i)==ch){
//Stores index of ch's location to loc
loc[x]=i;
x++; //Increment for each time ch was counted in pStr
}
}
//Returns the number of times ch was found
return x;
}
如果这不太烦人的话,我已经包含了我的全部代码,如果有帮助的话,我可以尝试制作一个更简单的问题版本。我认为发布整个代码可能对回答问题更有用。
再次感谢,干杯!
while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n'){
printf("Enter a character to search: ");
search=getchar();
charsFound=CharIsAt(array,search,found,SIZE);
printf("Entered text: ");
fputs(array,stdout);
printf("Character being searched for: %c\n",search);
printf("Character found at %d location(s).\n",charsFound);
for (i=0;i<charsFound;i++)
printf("%c was found at %d\n",search,found[i]);
if (fgets(array,SIZE, stdin)==NULL) break;
}
return 0;
这应该有效
已发布代码的主要问题是用户必须按 enter
才能将 search
字符输入程序。然而,对 getchar()
的调用只消耗了一个字符,因此它没有消耗换行符序列。
要解决此问题,请循环调用 getchar()
直到字符为 EOF 或 '\n' 以清空 stdin
的 any/all 剩余垃圾。
然后回到循环的顶部
出于某种原因,一旦我输入要搜索的字符,main 中的 while 循环就会终止,但目的是让您能够输入一行,然后输入一个字符进行搜索,直到您输入一个空行(输入没有什么)。基本上我想无限地执行第 1 步和第 2 步,直到我什么都不输入并按回车键。为什么这不起作用?感谢任何人的帮助!
另外,一个小问题,输入字符进行搜索后如何清除垃圾?
#include <stdio.h>
#define SIZE 41
int CharIsAt(char *pStr,char ch,int loc[],int mLoc);
int main(void){
char array[SIZE],search;
int found[SIZE],i,charsFound;
//Step 1
printf("Enter a line of text(empty line to quit): ");
while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n'){ //Loop until nothing is entered
//Step 2
printf("Enter a character to search: ");
search=getchar();
charsFound=CharIsAt(array,search,found,SIZE);
printf("Entered text: ");
fputs(array,stdout);
printf("Character being searched for: %c\n",search);
printf("Character found at %d location(s).\n",charsFound);
for (i=0;i<charsFound;i++)
printf("%c was found at %d\n",search,found[i]);
printf("Enter a line of text(empty line to quit): ");
}
return 0;
}
int CharIsAt(char *pStr,char ch,int loc[],int mLoc){
//Searches for ch in *pStr by incrementing a pointer to access
//and compare each character in *pStr to ch.
int i,x;
for (i=0,x=0;i<mLoc;i++){
if (*(pStr+i)==ch){
//Stores index of ch's location to loc
loc[x]=i;
x++; //Increment for each time ch was counted in pStr
}
}
//Returns the number of times ch was found
return x;
}
如果这不太烦人的话,我已经包含了我的全部代码,如果有帮助的话,我可以尝试制作一个更简单的问题版本。我认为发布整个代码可能对回答问题更有用。
再次感谢,干杯!
while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n'){
printf("Enter a character to search: ");
search=getchar();
charsFound=CharIsAt(array,search,found,SIZE);
printf("Entered text: ");
fputs(array,stdout);
printf("Character being searched for: %c\n",search);
printf("Character found at %d location(s).\n",charsFound);
for (i=0;i<charsFound;i++)
printf("%c was found at %d\n",search,found[i]);
if (fgets(array,SIZE, stdin)==NULL) break;
}
return 0;
这应该有效
已发布代码的主要问题是用户必须按 enter
才能将 search
字符输入程序。然而,对 getchar()
的调用只消耗了一个字符,因此它没有消耗换行符序列。
要解决此问题,请循环调用 getchar()
直到字符为 EOF 或 '\n' 以清空 stdin
的 any/all 剩余垃圾。
然后回到循环的顶部