What was I doing using strncmp?

derp.
This commit is contained in:
Dan Buch 2012-05-06 23:31:45 -04:00
parent f4f658a7f7
commit 6279bfa4bf

View File

@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double c2f(double celsius) {
@ -14,7 +13,7 @@ double f2c(double fahrenheit) {
int die_usage(const char *msg) {
printf("Usage: temperatures [FC]<temp>\n");
printf("Usage: temperatures <temp>[fFcC]\n");
printf("%s\n", msg);
exit(1);
}
@ -27,20 +26,23 @@ int main(int argc, char * argv[]) {
char msg[255];
double temp;
char scale = '\0';
const char * temp_string = argv[1];
size_t temp_len = strlen(temp_string);
if (strncmp(temp_string, "F", temp_len) > -1) {
sscanf(temp_string, "F%lf", &temp);
printf("%.2lf\n", f2c(temp));
} else if (strncmp(temp_string, "C", temp_len) > -1) {
sscanf(temp_string, "C%lf", &temp);
printf("%.2lf\n", c2f(temp));
} else {
sprintf(msg, "Argument '%s' does not match '[FC]<temp>'",
temp_string);
die_usage(msg);
sscanf(temp_string, "%lf%c", &temp, &scale);
switch (scale) {
case 'c':
case 'C':
printf("%.2lf\n", c2f(temp));
break;
case 'f':
case 'F':
printf("%.2lf\n", f2c(temp));
break;
default:
sprintf(msg, "Unknown scale '%c'", scale);
die_usage(msg);
break;
}
return 0;