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;
}