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