Home > Back-end >  C language gets function does not work
C language gets function does not work

Time:09-28

# include & lt; stdio.h>
# include & lt; String. H>
Int main ()
{
Int n, I;
The scanf (" % d ", & amp; N);
The char b [80], min [80].
getchar();
Gets (min).
for(i=1; I{
Gets (b);
If (STRCMP (min, b) & gt; 0)
Strcpy (min, b);
}
Min is: printf (" % s ", Min);
return 0;
}


# include & lt; stdio.h>
# include & lt; String. H>
The main ()
{
Int N, I, a;
Char STR [1000] [80], * c.
The scanf (" % d \ n ", & amp; N);
for(i=0; IGets (STR [I]);
C=STR [0].
for(i=1; I{
If (STRCMP (c, STR [I]) & gt; 0)
C=STR [I];
}
Min is: printf (" % s ", c);
}
Why don't the second paragraph add getchar can also normal execution, and the first period of not less execution gets

CodePudding user response:

Gets is met a newline returned, so the first if not getchar, then input after n '\ n' will be gets (first) receiving, return directly,

CodePudding user response:

reference 1/f, confident boy reply:
gets met a newline is returned, so the first if not getchar, then input after n '\ n' will be gets (first) receiving, direct return,
should have getchar, I also have this problem, instructed the

CodePudding user response:

In the console through the keyboard input data, in order to enter as end mark, when the end of the input, the keyboard input data, together with the enter key is input to the input buffer, in the program the first call to getchar () function to read a byte of data from the input buffer, it is important to note that the if at this point in a program for the second time calls getchar () function, because as the enter key data in the input buffer is not read , the second getchar () function to read is a carriage return,
Above is adapted from "c + + getchar () method of use"
Wish I could help you!
Another two code, your n and n didn't specify a value, how can the for loop up?
  • Related