Hi there, Folks. I've been going through Bruce Eckles's "Thinking in C" flash-based tutorial.
It's a little annoying as he uses unexplained commands in his answers, which means it's almost impossible to complete the exercise if you're not already familiar with C!
Anyhow.. I finally figured out how the following works, but I still don't understand why I need a getchar();...
Code:
for (n = 0; n < MAXEMPS; ++n) {
printf("Enter last: "); fflush(stdout);
gets(emps[n].last);
if (strlen(emps[n].last) == 0)
break;
printf("Enter first: "); fflush(stdout);
gets(emps[n].first);
printf("Enter title: "); fflush(stdout);
gets(emps[n].title);
printf("Enter salary: "); fflush(stdout);
scanf("%d", &emps[n].salary);
getchar(); /* eat newline */
}
I'm reading in data from the keyboard to fill in an Employee Struct (last name, first name, title, and salary). When the user enters a blank line (only \n) then it is supposed to stop (thus the if break test).
But I don't understand why I need the getchar(); at the end there. Where is that EXTRA \n coming from? Is it returned form scanf?
Thanks in advanced. I just want to understand what's going on in the code.
Cheers,
Jeffrey