0

I am currently tasked with writing a code for a resistance decoder that reads from a file.

specifically, my code has to read from a file input that has three bands of color separated by a coma: input.txt

red, green, blue
black, orange, yellow

it'll then have to decode those colors with a set of values for each color starting with 0 all the way to 9

lastly, it has to calculate how much resistance each color adds up to multiplied by 10^n

For example, if the first band is red, the second is green, and the third is blue, the resistor has a value of 50 × 103 ohms.

EDIT: Here's my code now, I still can't figure out how to pass the values from my strtok to the function, I'm not really good with pointers it confuses the hell out of me along with file handling, so I don't really know what's going on in my loop (in fact it was my prof's loop code that she gave us to get started) I've barely had any practice on those topics since uni. was moving too fast and all so I don't have much experience with it.

An explanation on my strtok loop and how I can pass its values to the function would be nice and some examples to better understand it would be great!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define size 100

double DecodeString(char *inputString){
  const char *kColorTable[10] = {
    "black", "brown", "red", "orange", "yellow", 
    "green", "blue", "violet", "gray", "white"
  };

  int i;

  for(i=0; i<10; i++){
    if(strcmp(inputString, kColorTable[i]) == 0){
      return (double)i;
    }
  }
  return -999.0;
}

int main(){
  char code;
  char color[size], *token, code1, code2, code3, *inputString;
  double resistance, color1, color2, color3;
  double value = DecodeString(inputString);


  FILE *fptrin, *fptrout;
  if(((fptrin = fopen("input.txt", "r"))==NULL) || ((fptrout = fopen("output.txt", "w")) == NULL)){
    printf("Error 404: File not found");
    exit(1);
  }

  while(fgets(color, size - 1, fptrin)){
    token = strtok(color, ",");
    puts("");
    while(token != NULL){
      printf("%s",token);
      token = strtok(NULL, ",");
      DecodeString(color);
    }
  }

  if (color1 == -999.0 || color2 == -999.0 || color3 == -999.0){
    printf("\n\nBad code -- cannot compute resistance\n");
  }

  else{
    resistance = (10.0 * color1 + color2) *pow(10.0, color3);

    if(resistance > 1000){
      printf("\n\nResistance in Kilo-ohms: %f\n",resistance);
    }
    else{
      printf( "\n\nResistance in ohms: %f\n",resistance);
    }
  }

  fclose(fptrin);
  fclose(fptrout);

  getchar();
  return 0;
}

I've been doing some practicing and testing based on the function you've given me, but I still can't quite figure out how to make things work. I've hit a complete total roadblock here and I'm unsure of what to do next :/

#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#define size 100

double DecodeString(char *inputString)
{
     const char *kColorTable[10] =
     {"black", "brown", "red", "orange", "yellow",
      "green", "blue", "violet", "gray", "white"};
     int i;

     for(i=0; i<10; i++)
     {
         if(strcmp(inputString, kColorTable[i]) == 0)
         {
             return (double)i;
         }
     }

     return -999.0;
}

int main(void){
  char color[size], *token, *inputString;
  double value;

  FILE *fptrin, *fptrout;
  if(((fptrin = fopen("input.txt", "r"))==NULL) || ((fptrout = fopen("output.txt", "w")) == NULL)){
    printf("Error 404: File not found");
    exit(1);
  }

  while(fgets(color, size - 1, fptrin)){
    token = strtok(color, ",");
    DecodeString(color);
    puts("");
    while(token != NULL){
      printf("%s",token);
      token = strtok(NULL, ",");
    }
    //printf("color value: ");
    printf("%f\n", value);
  }
    return 0;
}
  • 1
    You should look into [proper C formatting](//prohackr112.tk/r/proper-c-formatting). Or learn how to [thoroughly obfuscate your code](//prohackr112.tk/r/proper-c-obfuscation). – MD XF Dec 02 '16 at 23:28
  • You need code that translates the string coming in from the strtok loop into a value from 0 to 9. Right now `code` is unused. Consider setting up a table of string names and compare against each string in order. Line the strings up such that there placement in the table is their value. Then, when you loop through the possible strings, the index of the loop will be the integer value you need for your math. – Michael Dorgan Dec 02 '16 at 23:34
  • In addition to the previous comment, you are comparing the return value of `fgets` (which returns a pointer to a char) with `EOF`, which is an int. I think you need to compare it will `NULL` instead. – David Choweller Dec 02 '16 at 23:36
  • Also, `fgets` does not take format specifiers like "%s". Perhaps you mean to use `sscanf`. If you mean to use `fgets`, its second parameter is an `int` specifying the size of your input buffer, not a string. – David Choweller Dec 02 '16 at 23:39
  • Also, your check for a non-existent input file does not work. You should change it to `if(((fptrin = fopen("input.txt", "r"))==NULL) || ((fptrout = fopen("output.txt", "w")) == NULL)){` – David Choweller Dec 02 '16 at 23:54

1 Answers1

2

I've included a mini function that takes a string and parses it into a double value for you. This is part of your missing logic. The rest, I leave to you.

#include <strings.h>

double DecodeString(char *inputString)
{
     const char *kColorTable[10] = 
     {"black", "brown", "red", "orange", "yellow", 
      "green", "blue", "violet", "gray", "white"};
     int i;

     for(i=0; i<10; i++)
     {
         if(stricmp(inputString, kColorTable[i]) == 0)
         {
             return (double)i;
         }
     }

     return -999.0;
}

int main(void)
{
    double value = DecodeString("blue");
    printf("%f\n", value );
    return 0;
}
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • I edited my code a bit to properly read the file now for the strtok loop, but I'm still not sure how I can pass its values to the function and back, haven't had much experience with pointers atm and uni. was moving too fast for me to catch up. An insight about my strtok loop and how it works right now as well as some example to help me better understand would be great! – Jefferson Ponce Dec 04 '16 at 03:10
  • `strtok` is a pretty complex piece of software. It splits a string into sub-strings based on the "token" string you pass as the second parameter. If you pass NULL into first, it keep splitting the last string passed in. Basically, your look is working its way through the string, breaking it up on each comma in the list. See http://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c for more info. – Michael Dorgan Dec 05 '16 at 18:22