How do I read both mixed numbers and fractions from stdin
? For example:
int whol, num, den;
scanf("%d %d/%d", &whol, &num, &den);
However, if the user wants to enter a non-mixed fraction, he has to put a "0" in the beginning.
How can I allow both forms: %d/%d
and %d %d/%d
?
Maybe I shouldn't be using scanf()
for this?
A preferred practice is to first read an entire line into a buffer first, and then parse it. For example,
The extra array
dummy
, which is never used, helps insure the complete input line is parsed.