Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,189 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,045 people online right now. Registration is fast and FREE... Join Now!




Smart English to Metric converter (C)

 
Reply to this topicStart new topic

Smart English to Metric converter (C), Creating Function

snowboarder6009
11 Sep, 2008 - 08:33 PM
Post #1

New D.I.C Head
*

Joined: 11 Sep, 2008
Posts: 16

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
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Smart English To Metric Converter (C)
11 Sep, 2008 - 10:58 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,166



Thanked: 45 times
Dream Kudos: 125
My Contributions
cpp

while (j < 1)
{
// when token[6] = meter
if (tokens[6] == *metricLength[0]);
{
// feet to meters
double result;

result = tokenNum * 0.3048;
j++;
printf("Feet to Meters %f\n", result);
// if you enter How many meters are in 5 feet, ANSWER: 1.524
}
// when token[6] = milli
if (tokens[6] == *metricLength[1])
{
//feet to millimeters
double result;

result = tokenNum * 304.8;
j++;
printf("Feet to Milli %f\n", result);
// if you enter How many milli are in 5 feet, ANSWER: 1524
}

}


I have just modified your if statements and didn't compiled it. So please check whether this thing works for you now.

I hope this will help you. smile.gif
User is online!Profile CardPM
+Quote Post

snowboarder6009
RE: Smart English To Metric Converter (C)
11 Sep, 2008 - 11:06 PM
Post #3

New D.I.C Head
*

Joined: 11 Sep, 2008
Posts: 16

Hey thanks for the help unfortunately it still gets stuck in the first IF statement and always wants to convert using meters.

Thanks again
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Smart English To Metric Converter (C)
11 Sep, 2008 - 11:20 PM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,166



Thanked: 45 times
Dream Kudos: 125
My Contributions
Well, sorry for overlooking biggest mistake. You are comparing strings using == !!! blink.gif
Use strcmp to compare strings, strings are nothing but character arrays in C and you cant compare them like that.
User is online!Profile CardPM
+Quote Post

AmitTheInfinity
RE: Smart English To Metric Converter (C)
11 Sep, 2008 - 11:28 PM
Post #5

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,166



Thanked: 45 times
Dream Kudos: 125
My Contributions
try something like this...
cpp

while (j < 1)
{
// when token[6] = meter
if (strcmp(tokens[6],metricLength[0])==0);
{
// feet to meters
double result;

result = tokenNum * 0.3048;
j++;
printf("Feet to Meters %f\n", result);
// if you enter How many meters are in 5 feet, ANSWER: 1.524
}
// when token[6] = milli
if (strcmp(tokens[6],metricLength[1])==0)
{
//feet to millimeters
double result;

result = tokenNum * 304.8;
j++;
printf("Feet to Milli %f\n", result);
// if you enter How many milli are in 5 feet, ANSWER: 1524
}

}


when you did this :tokens[6] == *metricLength[j] you were actually comparing first letter of your token with it. and as milli and meter both has m as first letter, so your first if was always true! smile.gif
User is online!Profile CardPM
+Quote Post

snowboarder6009
RE: Smart English To Metric Converter (C)
11 Sep, 2008 - 11:34 PM
Post #6

New D.I.C Head
*

Joined: 11 Sep, 2008
Posts: 16

Thank you AmitTheInfinity! That worked.

If I wanted to put that into a function, how would I go about doing that?

This post has been edited by snowboarder6009: 11 Sep, 2008 - 11:46 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 04:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month