My program keeps skipping scanf().
The second time i want to input a value into a variable instead of prompting the user for input it automatically inputs line feed/10.
What am i doing wrong and how can i correct it?
My program keeps skipping scanf().
The second time i want to input a value into a variable instead of prompting the user for input it automatically inputs line feed/10.
What am i doing wrong and how can i correct it?
use : fflush( stdin );
T
You can read a discussion on this on the bottom half of the 1st page here:
http://www.cprogramming.com/cboard/s...threadid=24711
Apparently the use of fflush(stdin) is a "sin"
Here's the answer in short:
while(getchar() != '\n');
Try not.
Do or do not.
There is no try.
- Master Yoda
It's a "sin" because it doesn't work right. Or rather, it may work. I have one compiler where it works. I have another where it doesn't. You know why that is? Because ANSI leaves flushing of input streams "undefined". This means whoever writes the compiler can make it do whatever they want, and still can call their compiler ANSI compliant.Originally posted by Cshot
You can read a discussion on this on the bottom half of the 1st page here:
http://www.cprogramming.com/cboard/s...threadid=24711
Apparently the use of fflush(stdin) is a "sin"
Here's the answer in short:
while(getchar() != '\n');
For example, I could make a compiler that formatted your hard drive when you use "fflush(stdin)". And I could still call my compiler ANSI compliant, because ANSI makes no specification on what happens when you flush an input stream.
Quzah.
Hope is the first step on the road to disappointment.
>Whenever you mix scanf with another input method (getchar(), >fgetc() or fgets()) you're in for some surprises.
except that in this case i'm only using scanf
>The safe way of getting input is to always use fgets(), then >examine the resulting buffer with sscanf - or whatever else >seems appropriate at the moment.
I'm not supposed to have learnt fgets() or sscanf yet.
No matter i've learnt more doing this program than in a year long course - So back to the books