How do I fill an 80-character buffer with characters as they are being entered or until the carriage return key is pressed, or the buffer is full, whichever occurs first.
<
#include
...
int count=0;
char buffer[81];
int ch=getchar();
while(count<80&&ch!='\n'&&ch!='\r'&&ch!=EOF){
buffer[count]=ch;
count=count+1;
ch=getchar();
}
buffer[count]='\0';
Once you have buffer
as a string, make sure you digest the rest of the line of input to get the input stream ready for its next use.
This can be done by the following code (taken from the scanf
section of this document):
scanf("%*[^\n]"); /* Skip to the End of the Line */
scanf("%*1[\n]"); /* Skip One Newline */