I am C beginner and I am trying to create a program that is working with user input. The problem is that when user inputs incorrect data, program ends in an infinite error loop (saying "Incorrect input data"). I tried fflush and clearing user input with empty string, but it just somehow doesnt work.
Main.c:
#include <stdio.h>
#include "nd.h"
#include "nsd.h"
int num1;
int num2;
int a;
//char *p;
int main()
{
while(!feof(stdin))
{
if(scanf("%d %d",&num1,&num2)==2)
{
if ((nd(num1)==1) && (nd(num2)==1))
{
printf("%s\n", "prime");
}
else
{
a=nsd(num1, num2);
printf("%d\n", a);
}
}
else
{
fprintf(stderr, "Incorrect input data...");
//fflush(stdin);
//scanf ("%s",p);
}
}
printf("%s\n", "DONE");
return 1;
}
nd.c:
#include "nd.h"
#include <math.h>
#include <stdlib.h>
int nd(int a) {
// calculate greatest divisor of "a"
// nd equals 1, if it is prime
int ret;
int min_sqrt;
if (a == 1) {
ret = 1;
} else {
min_sqrt = abs(a) / 2;
for (ret=min_sqrt; ret>1; ret--) {
if ((a % ret)==0) {
break;
}
}
}
return ret;
}