0

I'm having some problems with my code.

My program calculates the amount of resistance based on the color of the three bands coming from an input file, then printing out to an output file.

Example input file:

red, green, blue
green, gray, yellow

Example output file:

Resistance in ohms = 680
Resistance in kilo-ohms = 1420

However, every time I run the program it crashes. I've done some debugging and found that it has a problem with the yellow index from decodeString function giving it a NULL value. I've partially fixed that problem by passing the values to the function decodeString instead of using pointers and now it seems to work.

Right now I don't get the correct output that I'm expecting and I don't know where the error is coming from. The code I have runs, but doesn't give the correct output at the moment. I just don't know where to look anymore.

Think anyone can help me fix this? Or if there's anything that I might have done wrong please point it out and explain why it went wrong. That'll be greatly appreciated!

The commented lines were used for debugging.

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

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

  for(i=0; i<10; i++){
    //printf("\n>>%s,%s",inputString,kColorTable[i]);
    if(strcmp(inputString, kColorTable[i]) == 0){
      return i;
    }
  }
  return -1;
}

int main(){
  int i=0, colord[3]={0,0,0};
  char color[size], *token, *inputString;
  double resistance=0, 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, fptrin)!=NULL){
    token = strtok(color, ",");
    while(token != NULL){
      if(token[strlen(token)-1]=='\n')
        token[strlen(token)-1]='\0';
      colord[i] = DecodeString(token);
      //printf(">>%s:%d ",token,colord[i]);
      i++;
      token = strtok(NULL, ",");
      puts("");
    }

    //printf("<><>");
    if (colord[0] == -1 || colord[1] == -1 || colord[2] == -1){
      printf("\n\nBad code -- cannot compute resistance\n");
    }

    else{
      resistance = (10.0 * colord[0] + colord[1]) * pow(10.0, colord[2]);
    }

    printf("%f",resistance);
    if(resistance > 1000){
      fprintf(fptrout,"Resistance in Kilo-Ohms: %f",resistance);
    }

    else{
      fprintf(fptrout,"Resistance in Ohms: %f",resistance);
    }
  }

  //fclose(fptrin);
  //fclose(fptrout);

  getchar();
  return 0;
}

So I tried to debug my program to find out what's going on and this is the result that I get.

blue,black
blue,brown
blue,red
blue,orange
blue,yellow
blue,green
blue,blue
red,black
red,brown
red,red
,blackn
,brownn
,redown
,orange
,yellow
,greenn
,bluewn
,violet
,graywn
,whiten

12
  • 1
    file not found is going to cause confusion when the issue is lack of permission. Try if(fopen(path, "r") == NULL { perror(path) ... Commented Dec 12, 2016 at 14:02
  • Can you give an example with expected result and what you get instead? One obvious thing: When outputting in kOhms, you should divide the number by 1000. Commented Dec 12, 2016 at 14:04
  • Aside: if you add the newline to the token separators string, that will clean up the code a little - strtok(color, ",\n"); and strtok(NULL, ",\n"); Commented Dec 12, 2016 at 14:05
  • If there are more than 3 colour bands colord[i] will break the array. Best to limit i. Commented Dec 12, 2016 at 14:09
  • Your input examples shows spaces between colors, but your code expects no spaces. Commented Dec 12, 2016 at 14:12

2 Answers 2

1

The first mistake in the code I see is that you are not removing the spaces from the input string, which you can do by changing the token separator string to " ,". You could also simplify the code a bit by removing the newline at the same time.

It is also prudent to limit the range of i since any line with more than 3 colours will break the array colord[], and this would have drawn your attention to the second mistake, which is you forgot to reset i within the loop, and this could explain why you are getting crashes.

while(fgets(color, size, fptrin) != NULL) {
    i = 0;                                  // reset `i`
    token = strtok(color, " ,\n");          // test for space and newline
    while(token != NULL && i < 3) {         // test `i` too
        colord[i] = DecodeString(token);
        i++;
        token = strtok(NULL, " ,\n");       // test for space and newline
    }
}

Finally you should divide by 1000 when displaying kOhms.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm actually not entirely done with the whole program, I'll deal with checking wether there's an invalid color, meaning a color not in the array, or if there's less than 2 or more than 3 colors, right now, I'm just trying to make sure that it reads and calculates right. I tried this and still I get bad code.
my output...it prints bad code whenever the wrong value gets passed. At this point I'm quite stuck.
What do mean by "bad code"? Do you mean the message "Bad code -- cannot compute resistance" when an invalid color is in the file. Isn't that correct?
Have I answered your question, or are you changing the question? My test version produces the correct answers from your given input.
BTW your example outputs are incorrect, the colours you show should be 25000 K and 580 K.
|
0

When you use

token = strtok(color, ",");

you only split on "," but on the file you have also a space after it so it should probably be

token = strtok(color, ", ");

or remove the spaces from the file

Also for the kilo-ohms i think you forgot a /1000 in the print

if(resistance > 1000){
  fprintf(fptrout,"Resistance in Kilo-Ohms: %f",resistance/1000);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.