My assignment is to fix the code. I have my edited code below and the original code below that. I figure I still have a few errors in here. My error checking doesnt seem to w
A few issues:
1. You should be using '9' and '0', since you want the ASCII values for digit '9' (0x39) and '0' (0x30), not 0x9 and 0x0.
if ( (c>'9') || (c<'0') ) {
2. != has higher precedence than =, so you need parens. Learn operator precedence, and if you're in doubt, use parens:
3. getchar
is a function not a variable.
while ((c = getchar()) != '\n') {
4. You use the wrong conversion. num
is a double, so you would need %f
. Or, you could make num
a int
.
printf("Please input number %f: ", num);
5. You never actually use c in any way (except error checking). You always return 0 or num
(see your else clause), which makes no sense. The else body of the original is correct.