What was I doing using strncmp?
derp.
This commit is contained in:
parent
f4f658a7f7
commit
6279bfa4bf
@ -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);
|
||||
sscanf(temp_string, "%lf%c", &temp, &scale);
|
||||
switch (scale) {
|
||||
case 'c':
|
||||
case 'C':
|
||||
printf("%.2lf\n", c2f(temp));
|
||||
} else {
|
||||
sprintf(msg, "Argument '%s' does not match '[FC]<temp>'",
|
||||
temp_string);
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
printf("%.2lf\n", f2c(temp));
|
||||
break;
|
||||
default:
|
||||
sprintf(msg, "Unknown scale '%c'", scale);
|
||||
die_usage(msg);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user