What was I doing using strncmp?

derp.
cat-town
Dan Buch 13 years ago
parent f4f658a7f7
commit 6279bfa4bf

@ -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));
} else if (strncmp(temp_string, "C", temp_len) > -1) {
sscanf(temp_string, "C%lf", &temp);
printf("%.2lf\n", c2f(temp)); printf("%.2lf\n", c2f(temp));
} else { break;
sprintf(msg, "Argument '%s' does not match '[FC]<temp>'", case 'f':
temp_string); case 'F':
printf("%.2lf\n", f2c(temp));
break;
default:
sprintf(msg, "Unknown scale '%c'", scale);
die_usage(msg); die_usage(msg);
break;
} }
return 0; return 0;

Loading…
Cancel
Save