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