3

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.

New contributor
Zachary Vandermude is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
25
  • 1
    You're missing, uh, your actual code - we typically do not debug verbose descriptions that can be explained in code instead. Commented yesterday
  • I just added the code, my bad Commented yesterday
  • 2
    Why are you assigning chars you want to discard to product[i].name? And then you check c? Just use c in both places, and you got what you need. Commented yesterday
  • 1
    It's unclear why you use fflush with fgets. Commented yesterday
  • 1
    Re "I cannot compare an int to the new line charecter", Yes you can. char and int can be compared against each other. But '\n' is also an int, so you'd be comparing an int with an int. Commented yesterday

1 Answer 1

4

To skip the rest of the line, you can use the following:

int c;
while ( ( c = getchar() ) != '\n' && c != EOF ) {
   // Do nothing, just discard
}

An equivalent but less compact version:

while ( 1 ) {
   int c = getchar();
   if ( c == '\n' || c == EOF )
      break;
}

It doesn't make sense to assign a character you want to discard to product[i].name. And you were checking a different variable (c) than you were setting (product[i].name).

Sign up to request clarification or add additional context in comments.

7 Comments

What you said at the end about disregarding makes sense, but to kind of follow-up on what something mentioned early, if getchar returns an integer, how can getchar ever be used to check a \n character or EOF? Do those 'things' have assigned integer values from the ASCII library?
The char type does not hold characters. The values of the char type are integers, like 0, 1, 2, 3,… Characters are encoded as integers, so “a” is 96 in ASCII, the most commonly used encoding scheme. The char type is big enough to hold any value of a character in the C implementation’s basic execution character set. The int type is at least as wide as the char type and is usually wider. The char type is usually not wide enough to hold the EOF value, but int can (almost always). So you should use int c; with c = getchar();, not char c.
Thank you for the answer. I am doing more research into how getchar() does not take any parameters and manages to read the input buffer. It is actually a very deep topic and ends down in a very cool rabbit hole of taking reading from the cpu. But anyways, that is very far out of scope of this initial question. I appreciate your time.
Also, getchar returns the encoding of a character as an unsigned value, so unsigned char would be better than char. But you still need int to accommodate EOF.
'\n' is just another way of writing 10 (on an ASCII-based machine). You can do 10 == 10, right?
"An equivalent but less compact and much easier to understand version"
And it also makes handling EOF (an end of file or error) differently than a LF easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.