this is my code below. What it does is not the issue. The issue is that once it is run, I put in my input and if its too small it will ask for input again on the second line which appears to have no affect the flow of my program. If I fill the buffer (which I'm assuming 100 or more) then I am not asked for a second prompt.
#include <stdio.h>
#include <string.h>
int main()
{
int ch;
char x[3];
char *word, string1[100];
x[0]='y';
while(x[0]=='y'||x[0]=='Y')
{
fgets(string1, 100, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
printf("The string is: %s", string1);
word = strtok(string1, " ");
while(word != NULL)
{
printf("%s\n", word);
word = strtok(NULL, " ");
}
printf("Run Again?(y/n):");
fgets(x, 2, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
}
return 0;
}
EDIT: I have replaced,
fgets(string1, 100, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
With,
fgets(string1, 100, stdin);
if (string1[98] != '\n' && string1[99] == '\0')
{
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
}
From the man page:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops
after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the
last character in the buffer.
fgets
will place all input, up to 99 chars
, into string1. If you input 98 characters and hit enter (creating a 99th \n
), then all 100 will be used as the last one is a \0
terminator.
Then you fall into that small while
loop which does nothing but consume another line of input. If you input less than the max for your string, then input is halted while that loop waits for a \n
.
If you input >98 characters, then the first 99 are saved to your input string, and the remainder along with that final \n
are immediately run through that while loop, causing it to exit quickly enough that it may seem to be skipped.
I hope that helps. Unfortunately I can't comment and ask for clarification, so I'll say here that it's a little difficult to tell what exactly you want fixed or made clear.