I've searched the forum for the solution, but still in confusion about the output my code produces.
So, the program is pretty simple.
It gets two numbers at the input until reaches the end of file.
If the input is bad, it should print error to stdout and proceed to the next pair.
If both are primes, it prints out prime
. Otherwise, it prints their GCD.
Problem is, that if the input is bad, i.e either one or both numbers are not actually numbers, the program skips scanf and keeps on printing error to stderr.
Yet, during debugging, I found out that all the next iterations scanf()
goes through, it returns 0
as if nothing was inputted at all.
And the prompt is inactive for inputting, since the program constantly prints to stderr.
nd
and nsd
are functions returning greatest divider and greatest common divisor respectively.
The main program is following:
#include <stdio.h>
#include "nd.h"
#include "nsd.h"
int main()
{
int a;
int b;
int inp_status = 0;
while (1){
inp_status=scanf(" %d %d", &a, &b);
if (inp_status == EOF){
break;
} else if (inp_status < 2){
fprintf(stderr, "Error: bad input\n");
} else {
if (a == 1 || b == 1){
printf("1\n");
} else if (nd(a) == 1 && nd(b) == 1){
printf("prime\n");
} else {
printf("%d\n",nsd(a,b));
}
}
}
fprintf(stderr, "DONE\n");
return 0;
}