Not bad at all for the first program.
Typical mistakes
Don't use
gets
. It is dangerous dangerous and deprecated. Usefgets
instead.Globals are evil. Try to abstain from them. Learn how to pass parameters and return values.
Advanced mistake
extractNumbers
doesn't do exactly what it claims. Instead it parses the input string, so calling it parse()
seems more appropriate. Also I'd recommend to structure it a bit differently, to emphasize a correct structure of input.
Your calculator accepts strings in form of number, opcode, number
, so the parsing procedure should do exactly that
In pseudocode,
parse_input(string)
num1 = parse_number(string)
opcode = parse_opcode(string)
num2 = parse_number(string)
return (num1, opcode, num2)
Of course, all parsing routines need to adjust the string for characters it consumed, they should be able to report problems, etc. For parsing numbers, C standard library provides strtol
family of functions. Returning multiple values shall also be addressed.
Not bad at all for the first program.
Typical mistakes
Don't use
gets
. It is dangerous and deprecated. Usefgets
instead.Globals are evil. Try to abstain from them. Learn how to pass parameters and return values.
Advanced mistake
extractNumbers
doesn't do exactly what it claims. Instead it parses the input string, so calling it parse()
seems more appropriate. Also I'd recommend to structure it a bit differently, to emphasize a correct structure of input.
Your calculator accepts strings in form of number, opcode, number
, so the parsing procedure should do exactly that
In pseudocode,
parse_input(string)
num1 = parse_number(string)
opcode = parse_opcode(string)
num2 = parse_number(string)
return (num1, opcode, num2)
Of course, all parsing routines need to adjust the string for characters it consumed, they should be able to report problems, etc. For parsing numbers, C standard library provides strtol
family of functions. Returning multiple values shall also be addressed.
Not bad at all for the first program.
Typical mistakes
Don't use
gets
. It is dangerous and deprecated. Usefgets
instead.Globals are evil. Try to abstain from them. Learn how to pass parameters and return values.
Advanced mistake
extractNumbers
doesn't do exactly what it claims. Instead it parses the input string, so calling it parse()
seems more appropriate. Also I'd recommend to structure it a bit differently, to emphasize a correct structure of input.
Your calculator accepts strings in form of number, opcode, number
, so the parsing procedure should do exactly that
In pseudocode,
parse_input(string)
num1 = parse_number(string)
opcode = parse_opcode(string)
num2 = parse_number(string)
return (num1, opcode, num2)
Of course, all parsing routines need to adjust the string for characters it consumed, they should be able to report problems, etc. For parsing numbers, C standard library provides strtol
family of functions. Returning multiple values shall also be addressed.
Not bad at all for the first program.
Typical mistakes
Don't use
gets
. It is dangerous and deprecated. Usefgets
instead.Globals are evil. Try to abstain from them. Learn how to pass parameters and return values.
Advanced mistake
extractNumbers
doesn't do exactly what it claims. Instead it parses the input string, so calling it parse()
seems more appropriate. Also I'd recommend to structure it a bit differently, to emphasize a correct structure of input.
Your calculator accepts strings in form of number, opcode, number
, so the parsing procedure should do exactly that
In pseudocode,
parse_input(string)
num1 = parse_number(string)
opcode = parse_opcode(string)
num2 = parse_number(string)
return (num1, opcode, num2)
Of course, all parsing routines need to adjust the string for characters it consumed, they should be able to report problems, etc. For parsing numbers, C standard library provides strtol
family of functions. Returning multiple values shall also be addressed.