-
help with getchar lol
can someone tell me what the heck is with getchar? it skips over the getchars and its REALLY annoying. also, should i use scanf to hold screen?? i was using getchar until all the time until i started using it for input aswell, then this error started happening
i just copy pasted a bunch of headers cause i was lazy and whipped this up fast, dont try running it without looking at the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
int bob,fred,joe, hold;
char c;
printf("enter bob\n");
scanf("%d", &bob);
printf("paused\n");
getchar(); /*why does it skip?*/
printf("enter fred\n");
scanf("%d", &fred);
printf("paused\n");
getchar(); /*why does it skip?*/
printf("enter joe\n");
scanf("%d", &joe);
printf("paused\n");
getchar(); /*why does it skip?*/
printf("bob %d fred %d joe %d\n",bob,fred,joe);
getchar(); /*why DOESN'T it skip?*/
printf("Problem child 2\n");
c = getchar();
printf("%c \n", c);
getchar(); /*why does it skip second getchar*/
printf("Problem child 3\n");
scanf("%d", &bob);
scanf("%d", &fred);
scanf("%d", &joe);
printf("bob %d fred %d joe %d\n",bob,fred,joe);
scanf("%d",hold);
return 0;
}
-
The input buffer has data left in it. Try flushing the buffer
and use code tags when posting code.
-
thanks, ill try that out. also, im really new to this forum, is there anywhere that posts all the html tags or is it just normal hyper text?
-
i really hate to be a mindless boob that just copies someones code and thinks nothing of it so, can some please explain how
Code:
while ((ch = getchar()) != '\n' && ch != EOF);
clears the input buffer? why is ch an integar? does ch serve any real purpose?
-
>clears the input buffer?
It reads characters until there's supposedly nothing left. This code assumes that a newline marks the end of input. In buffered systems taking user input, that assumption is usually correct.
>why is ch an integar?
The value of EOF may not be representable by a char.
>does ch serve any real purpose?
Yes, it lets you test for both EOF and a newline with only one call to getchar.
-Prelude
-
thanks alot, and also, in your sig you should throw in
/*that, is executable*/ to substitute for "that is the question"