I'm trying to learn C, and im having problems with a few loops. For example, I'll use my program that finds multiples of 6:
#include <stdio.h>
int main()
{
int i;
char ch;
/* display all multiples of 6 */
for(i=1; i<10000; i++){
if(!(i%6)){
printf("%d, more? y/n ", i);
ch= getchar();
if(ch=='n') break; /* stop the loop */
printf("\n");
}
}
}
Now it compiles cleanly, but something funny happens. The first time, the loop will function properly, now on the second time, (i think) it will skip the 'ch=getchar();' and then repeat again. The third time, it will function normally. And so on. The output looks like this.
6, more? y/n y
12, more? y/n
18, more? y/n y
24, more? y/n
30, more? y/n y
36, more? y/n
42, more? y/n n
Does anyone know why it doesn't ask for a character for the even loop iterations?