One more goofy modification to temperatures practice prog

before moving on to something more fun.
This commit is contained in:
Dan Buch 2012-05-29 16:52:13 -04:00
parent 2599a100da
commit f5902e699f

View File

@ -14,11 +14,12 @@ double f2c(double fahrenheit) {
} }
void usage(const char *prog, const char *msg) { void die_usage(const char *prog, const char *msg) {
fprintf(stderr, "Usage: %s <temp>[fFcC]\n", prog); fprintf(stderr, "Usage: %s <temp>[FC]\n", prog);
if (msg != NULL) { if (msg != NULL) {
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
} }
exit(EXIT_FAILURE);
} }
@ -33,8 +34,7 @@ int main(int argc, char * argv[]) {
const char *temp_string; const char *temp_string;
if (argc < 2) { if (argc < 2) {
usage(prog, NULL); die_usage(prog, NULL);
exit(EXIT_FAILURE);
} }
if (((int)strlen(argv[1]) == 1) && (argv[1][0] == '-')) { if (((int)strlen(argv[1]) == 1) && (argv[1][0] == '-')) {
@ -45,28 +45,23 @@ int main(int argc, char * argv[]) {
} }
if ((ret == 0) || (ret == EOF)) { if ((ret == 0) || (ret == EOF)) {
usage(prog, "No temperature provided!"); die_usage(prog, "No temperature provided!");
exit(EXIT_FAILURE);
} }
if (ret == 1) { if (ret == 1) {
usage(prog, "No scale provided!"); die_usage(prog, "No scale provided!");
exit(EXIT_FAILURE);
} }
switch (scale) { switch (scale) {
case 'c':
case 'C': case 'C':
printf(out_fmt, c2f(temp), 'F'); printf(out_fmt, c2f(temp), 'F');
break; break;
case 'f':
case 'F': case 'F':
printf(out_fmt, f2c(temp), 'C'); printf(out_fmt, f2c(temp), 'C');
break; break;
default: default:
sprintf(msg, "Unknown scale: '%c'", scale); sprintf(msg, "Unknown scale: '%c'", scale);
usage(prog, msg); die_usage(prog, msg);
break;
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);