Just clear the stream of extra characters. You can use this function:
void discard ( FILE *in )
{
int c;
while ( ( c = fgetc ( in ) ) != EOF && c != '\n' )
;
}
DID YOU KNOW THAT:
The c-defined 'stdin' that takes the input directly from the keyboard is compatible with (if not a direct type of) FILE* ?
AND NOW YOU KNOW!
What's going on here? I'll walk you through it.
void discard ( FILE *in ) //1
{
int c;//2
while ( ( c = fgetc ( in ) ) != EOF) && c != '\n' ) //3
;
}
Okay, here goes:
1. The function name and accepted parameters. In this case, you'll want to call:
when you want to clear the input stream of excess characters.
You'd call it after you get 80 digits.
2. This is the digit you want to get in for your input.
3. This while loop does everything in the declaration of the while loop.
Its first argument, ((c = fgetc(in)) != EOF), does two things:
First, it calls the fgetc function and returns an integer value, which is stored in the variable 'c'. The parenthesis around
are to help both you and the compiler realize that the fgetc function and assignment should be called first.
Second, it compares the value of 'c' to the defined value of the end of file character (abbreviated as EOF). If 'c' has the value of EOF, the while loop exits.
That's what the !=EOF does.
{Note: "in" is the reference the function uses instead of stdin, the keyboard input. It's the same thing, just renamed for the discard function.}
The second argument in the while loop,
Compares the value of c (taken in the first argument) to the value of the "newline" character. When using stdin, this translates to a press of the enter key.
The two arguments for the while loop are joined by the logical AND operator, &&. That means that as long as both conditions(c can't be the end of file and c can't be a new line) are met, any key press is discarded. Only pressing enter can break the loop.
So, once you've got your 80 digit entries, you call the discard function with stdin as the argument, and anything that's not an enter will be assigned and overwritten without getting into the numbers you want to preserve. Once enter is pressed, the discard function exits, and you can start entering the second 80 digit number.