-
Why does it hang?
Code:
#include <stdio.h>
unsigned int gcd(unsigned int a, unsigned int b) {
unsigned int t;
while (b) {
t = b;
b = a%b;
a = t;
}
return a;
}
void inFlush(FILE *in) {
int ch;
while ((ch = fgetc(in)) != EOF && ch != '\n') {
/* f l u s h i n g */
}
}
char *skipSpace(char *inStr) {
while (*inStr == ' ' || *inStr == '\t') {
++inStr;
}
return inStr;
}
int main() {
char buff[1024];
char *p;
unsigned int a, b;
while (1) {
printf("Enter value for a & b (q will quit): ");
fgets(buff, sizeof(buff), stdin);
inFlush(stdin);
p = skipSpace(buff);
if (*p == 'q') break;
sscanf(p, "%u %u", &a, &b);
printf("GCD of %u & %u is: %u\n\n", a, b, gcd(a, b));
}
return 0;
}
This program just hangs even when I enter 'q' just after running :(
Edit: it needs two <enter>s in a row to run further.
-