I have a piece of code here:
Tree *rangeprint(Tree *t) {
char first[20];
char last[20];
int f = 0;
int l = 0;
printf("First Entry?\n");
while(1) {
first[f] = getchar();
if(first[f] == '\n') {
first[f] = '\0';
break;
}
f++;
}
printf("Last Entry?\n");
while(1) {
last[l] = getchar();
if(last[l] == '\n') {
last[l] = '\0';
break;
}
l++;
}
printf("%s %s\n", first, last);
}
When I run this code, the output I get in the console is:
First Entry?
Last Entry?
Why is it skipping over the while loops and printing everything before executing them?
UPDATE - I changed the termination condition to 'x' instead of '\n' and sure enough it prints properly.
Adding a random getchar() before the loop starts fixes the problem, since the '\n' is read into there.