Originally Posted by
gratiafide
Code:
int a;
do{
a=scanf("%f", &rate);
f(!(rate<=5 && rate>=1))
If scanf() encountered something other than a number, a will be zero (or EOF if end of input), and rate will be unmodified, i.e. its previous value.
It does not make sense to check the value of rate if a is zero; so, you should check a first.
In fact, I'd just do
Code:
if (scanf("%f", &rate) < 1) {
printf("Invalid option. Exiting.\n");
break;
}
or, if you want to skip invalid lines,
Code:
if (scanf("%f", &rate) < 1) {
int c;
/* Skip this line, until end of input or newline. */
do {
c = getchar();
} while (c != EOF && c != '\n' && c != '\r');
/* No more input? */
if (c == EOF) {
printf("No more input. Exiting.\n");
break;
}
/* Complain about invalid option, then re-scan. */
printf("What? Dude, just numbers, please.\n");
continue;
}
Also, why don't you do the print just once, just before the scanf()? Seems pretty wasteful to duplicate printf() code like that.