I am writing a program that should read two lines, which may contain line breaks and various other characters. Therefore, I use EOF (Ctrl-Z or Ctrl-D) to complete the line.
This works well with the first variable, but with the second variable, however, it seems problematic, since it is obvious that something is stuck in the input buffer and the user cannot enter anything.
I tried to clear the buffer with while (getchar() != '\n'); and a few similar options, but nothing seemed to help. All attempts at cleaning led to an infinite loop, and without cleaning, adding a second variable is impossible.
The characters for both variables are read in a loop like this: while((c = getchar()) != EOF) , which means that it is EOF, that I am stuck in my buffer. Or does it somehow affect the behavior of the program? Is there something wrong with the logic used?
I begin to feel a little desperate after several hours of struggling with this.
The code:
#include <stdio.h>
c string newline eof getchar
user196316 Oct 25 '09 at 21:16 2009-10-25 21:16
source share