1

I'm trying to be able to allow my program to recognize what is being entered in to the token string and compare it to the metric measurement so I can make multiple conversions like kilo, centi, etc. The problem im running into is I cant get the program to recognize milli right now.

Also note that there is a function for converting english into pig latin so ignore some of the pig latin variables

int i = 0;
int j = 0;
int command = 0, //Pig latin ints
    count = 0;
double tokenNum;
char name[50];
char *tokens[10];
char *englishLength[] = {"feet"};
char *metricLength[] = {"meter", "milli", "centi", "deci", "deka", "hecto", "kilo"};    
char sentence_ar[100], //Pig latin chars
     *array_of_pointers_to_strings[50],
     new_string1[50] = {'\0'},
     new_string2[50] = {'\0'};




printf ("Enter conversion in the following format\n -- How many meters are in X feet --:\n ");
            fflush (stdin);
            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("%d\n", tokenNum);

            while (j < 1)
                {
                    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
                    }
            }
Joe Bray
  • 23
  • 1
  • 5

1 Answers1

2

Here are few observations which you can use (This is more of a compilation of the comments above):
1. Don't use fflush(stdin). fflush is for output stream only.
2. Don't use gets. Its not safe. Use fgets as suggested.
3. Add bound checks to your array. In while ((tokens[i] = strtok (NULL, " ")) != NULL), you should check for the value of i so that is does not exceed the allocated array size of tokens
4. In the statement if (strcmp(tokens[6],metricLength[0])==0);, the semicolon makes this condition result in empty body. I think adding the semicolon was not intended so get rid of it.
5. As per your input requirement, you are checking the wrong token. If your input requires "How many METRIC_UNIT are in VALUE ENGLISH_UNIT", then you should compare the third token i.e. tokens[2]. If may be a good idea to validate the input as well.
6. Crank up the warnings on your compiler to the max & fix them all! Its a good practice.
Hope this helps!

Community
  • 1
  • 1
another.anon.coward
  • 11,087
  • 1
  • 32
  • 38