I am encountering a problem inside a while loop that is embedded in a for loop iterating through a struct of arrays. The issues I am coming across is before the array iterates again, I want to have something to clear the \n character from the input stream so ffget does not read the \n character and effectively skips my attempt to enter a string.
while ((product[i].name = getchar()) != '\n' && c != EOF) {
// do nothing, just discard
}
While I was reading about how to use the fgets, I found out it will read a string up to the new line character and stop. This leaves two options to delete the \n, one that works really is fflush; which reads from the input buffer every \n and effectively deletes it.
I got it to work. But, I wanted to use getchar instead. It works similarly but it reads one character at a time from the input buffer which is why I am using the while loop. However, I was wondering, instead of creating a variable char c or something to that effect and passing that into the while loop, maybe I could just pass in the member of the struct that needs to have the \n checked for.
The issue is I am cannot pass in the member of the struct. I think there are two reasons why this will not work. First, getchar reads and returns a char. Second, I cannot pass a member of a struct into the conditions of a while loop (which I think is incorrect).
For what I have tried, I have used fflush and fgets in the typical fashion to remove \n character from the input buffer stream. I declared a character and passed the variable into the while loop and I have used fflush.
product[i].name? And then you checkc? Just usecin both places, and you got what you need.fflushwithfgets.charandintcan be compared against each other. But'\n'is also anint, so you'd be comparing anintwith anint.