Hello Everyone. I am working on making a English to Metric converter in C.
You enter the conversion you want in this format -- How many meters are in X feet.
I am now trying to make a function for the big IF statement.
I came up with
--
void metric_to_english_length (double tokenNum, char metricLength[], char tokens[10], int i)
--
Thank you to anyone who posts...online to explain if you have any questions
CODE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[50];
char *tokens[10];
int i = 0;
int j = 0;
double tokenNum;
char *englishLength[] = {"feet"};
char *metricLength[] = {"meter", "milli", "centi", "deci", "deka", "hecto", "kilo"};
printf ("Enter conversion in the following format -- How many meters are in X feet --: ");
gets (name);
printf ("Original name: %s\n", name);
tokens[0] = strtok (name, " ");
printf ("Token[0]: %s\n", tokens[0]);
i++;
while ((tokens[i] = strtok (NULL, " ")) != NULL)
{
printf ("Token[%d]: %s\n", i, tokens[i]);
i++;
}
tokenNum = atof (tokens[5]);
printf("%f\n", tokenNum);
// when token[6] = meter
if (strcmp(tokens[6],metricLength[0])==0);
{
// feet to meters
double result;
result = tokenNum * 0.3048;
printf("Feet to Meters %f\n", result);
// if you enter How many meters are in 5 feet, ANSWER: 1.524 WORKS
}
// when token[6] = milli
if (strcmp(tokens[6],metricLength[1])==0)
{
//feet to millimeters
double result;
result = tokenNum * 304.8;
printf("Feet to Milli %f\n", result);
// if you enter How many milli are in 5 feet, ANSWER: 1524 WORKS
}
else if (strcmp(tokens[6],metricLength[2])==0)
{
//feet to centi
double result;
result = tokenNum * 30.48;
printf("Centi to Feet %f\n", result);
// if you enter How many centi in 5 feet, ANSWER: 152.4 WORKS
}
else if (strcmp(tokens[6],metricLength[3])==0)
{
//feet to deci
double result;
result = tokenNum * 3.04800;
printf("deci to Feet %f\n", result);
// if you enter How many deci in 5 feet, ANSWER: 15.24 WORKS
}
else if (strcmp(tokens[6],metricLength[4])==0)
{
//feet to deka
double result;
result = tokenNum * 0.3048;
printf("deka to Feet &f\n", result);
// if you enter How many deka in 5 feet, ANSWER: 1.524 WORKS
}
else if (strcmp(tokens[6],metricLength[5])==0)
{
//feet to hecto
double result;
result = tokenNum * 0.003048;
printf("hecto to Feet %f\n", result);
// if you enter How many hecto in 5 feet, ANSWER: .01524 WORKS
}
else if (strcmp(tokens[6],metricLength[6])==0)
{
//feet to kilo
double result;
result = tokenNum * 0.0003048;
printf("kilo to Feet %f\n", result);
// if you enter How many kilo in 5 feet, ANSWER: .001524 WORKS
}
printf("token 6: %s\n", tokens[6]);
return 0;
}
This post has been edited by snowboarder6009: 12 Sep, 2008 - 07:15 AM