问题
the while loop is not working here.there is no compilation error and the print statement is also getting executed in the beggining.the code works fine without the while loop.the code is to print the most repeating letter in a string and the number of times it is repeated
#include <stdio.h>
#include <string.h>
int main(void)
{
int t;
int tempC;
scanf("%d",&t);
while ( (tempC = getchar()) != '\n' && tempC != EOF );
while(t--)
{
char c[100];
char r='z';
int i,j=0,count,a,k,amx;
gets(c);
k=strlen(c);
for(i=0,amx=1;i<k;i++)
{
if(c[i]!=0)
{
for(j=0,count=1;j<k;j++)
{
if(c[i]==c[j])
{
if(i!=j)
{
count++;
c[j]=0;
}
a=count;
if(a>amx||(a==amx&&(c[i]<r)))
{
amx=a;
r=c[i];
}
}
}
}
}
printf("%d %c\n",amx,r);
}
}
回答1:
After the following line is executed,
scanf("%d",&t);
the newline character is still left in the input stream. The next call to gets
ends up reading just the newline.
You need to add code to ignore the rest of the line after readning t
.
scanf("%d",&t);
// Ignore the rest of the line from the input stream.
char c;
while ( (c = getchar()) != '\n' && c != EOF);
Also, don't use gets
.
Further reading: Why is the gets function so dangerous that it should not be used?.
Use fgets instead.
回答2:
According to what you mentioned, while loop works fine and just use scanf() in place of gets()(Don't use gets()) and then printf() will also be executed after loop.
回答3:
You can also ask directly to your gcc compiler
:
gcc test.c -o test.x -std=c99 -Wall -Wextra
And to be sure, try other options: -std=gnu99
, -std=gnu89
, -std=c89
, and my preferred:
gcc test.c -o test.x -Wall -Wextra -ansi -pedantic-errors -O0
来源:https://stackoverflow.com/questions/31167941/when-compiled-it-is-showing-warningcannot-find-entry-symbol-rrorsdefaulting-to