Code:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
void get_colors(char (*scanned)[7]);
bool colors_validate(const char (*scanned)[7], const char (*colors)[7]);
void rvalue(const char (*scanned)[7], const char (*colors)[7]);
void tvalue(const char (*scanned)[7], const char (*colors)[7]);
void rtcvalue(const char (*scanned)[7], const char (*colors)[7]);
int main(void){
char scanned[6][7], answer;
char colors[12][7] = {"silver", "gold", "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"};
int i;
get_colors(scanned);
if(colors_validate(scanned, colors)){
rvalue(scanned, colors);
tvalue(scanned, colors);
rtcvalue(scanned, colors)
}
getch();
return 0;
}
void get_colors(char (*scanned)[7]){
int i, j;
printf("\nPlease enter 6 resistor colors: \n");
scanf("%s %s %s %s %s %s", scanned[0], scanned[1], scanned[2], scanned[3], scanned[4], scanned[5]);
while(getchar() != '\n');
for(j=0; j<6; j++){
for(i=0; i<7; i++){
scanned[j][i] = tolower(scanned[j][i]);
}
}
}
bool colors_validate(const char (*scanned)[7], const char (*colors)[7]){
int i, j;
for(i = 0; i < 12; i++){
for(j = 0; j < 6; j++){
if(strncmp(scanned[j], colors[i], 7) != 0){ return false; }
}
}
if(strncmp(scanned[0], colors[2], 7) == 0 || strncmp(scanned[0], colors[1], 7) == 0 || strncmp(scanned[0], colors[0], 7) == 0){ printf("Wrong color. Try again\n"); return false; }
else if(strncmp(scanned[1], colors[1], 7) == 0 || strncmp(scanned[1], colors[0], 7) == 0 || strncmp(scanned[2], colors[1], 7) == 0 || strncmp(scanned[2], colors[0], 7) == 0){ printf("Wrong color. Try again\n"); return false; }
else if(strncmp(scanned[4], colors[11], 7) == 0) { printf("Wrong color. Try again\n"); return false; }
else if(strncmp(scanned[5], colors[7], 7) == 0 || strncmp(scanned[5], colors[10], 7) == 0 || strncmp(scanned[5], colors[11], 7) == 0 || strncmp(scanned[5], colors[1], 7) == 0 || strncmp(scanned[5], colors[0], 7) == 0) { printf("Wrong color. Try again\n"); return false; }
else return true;
}
/* Find the Resistance value */
void rvalue(const char (*scanned)[7], const char (*colors)[7]){
int i;
double rv, rval, power;
for(i=0; i<4; i++){
if ( strncmp(scanned[i], colors[0], 7) == 0 ) { rv = -2; power = .01; }
else if ( strncmp(scanned[i], colors[1], 7) == 0 ) { rv = -1; power = .1;}
else if ( strncmp(scanned[i], colors[2], 7) == 0 ) { rv = 0; power = 1;}
else if ( strncmp(scanned[i], colors[3], 7) == 0 ) { rv = 1; power = 10;}
else if ( strncmp(scanned[i], colors[4], 7) == 0 ) { rv = 2; power = 100;}
else if ( strncmp(scanned[i], colors[5], 7) == 0 ) { rv = 3; power = 1000;}
else if ( strncmp(scanned[i], colors[6], 7) == 0 ) { rv = 4; power = 10000;}
else if ( strncmp(scanned[i], colors[7], 7) == 0 ) { rv = 5; power = 100000;}
else if ( strncmp(scanned[i], colors[8], 7) == 0 ) { rv = 6; power = 1000000;}
else if ( strncmp(scanned[i], colors[9], 7) == 0 ) { rv = 7; power = 10000000;}
else if ( strncmp(scanned[i], colors[10], 7) == 0 ){ rv = 8; power = 100000000;}
else if ( strncmp(scanned[i], colors[11], 7) == 0 ){ rv = 9; power = 1000000000;}
if(i == 0){ rval = rv * 100; }
if(i == 1){ rval += rv * 10; }
if(i == 2){ rval += rv; }
if(i == 3){ rval = rval * power; }
}
if(rval < 1000000){ printf("Resistance = %.2f kOhms\n", rval/1000); }
else { printf("Resistance = %.2f MOhms\n", rval/1000000); }
}
/* Find the resistance tolerance value */
void tvalue(const char (*scanned)[7], const char (*colors)[7]){
double tv;
if( strncmp(scanned[4], colors[3], 7) == 0){ tv = 1; }
else if( strncmp(scanned[4], colors[4], 7) == 0){ tv = 2; }
else if( strncmp(scanned[4], colors[5], 7) == 0){ tv = 3; }
else if( strncmp(scanned[4], colors[6], 7) == 0){ tv = 4; }
else if( strncmp(scanned[4], colors[7], 7) == 0){ tv = .5; }
else if( strncmp(scanned[4], colors[8], 7) == 0){ tv = .25; }
else if( strncmp(scanned[4], colors[9], 7) == 0){ tv = .1; }
else if( strncmp(scanned[4], colors[10],7) == 0){ tv = .05; }
else if( strncmp(scanned[4], colors[1], 7) == 0){ tv = 5; }
else if( strncmp(scanned[4], colors[0], 7) == 0){ tv = 10; }
if(tv > 0){ printf("Tolerance = +/- %.2f%%\n", tv); }
else { printf("Tolerance = +/- %.2f%%\n", tv); }
}
/* Find the resistance temperature coefficient */
void rtcvalue(const char (*scanned)[7], const char (*colors)[7]){
int rtc;
if(strncmp(scanned[5], colors[3], 7) == 0) { rtc = 100; }
else if(strncmp(scanned[5], colors[4], 7) == 0) { rtc = 50; }
else if(strncmp(scanned[5], colors[5], 7) == 0) { rtc = 15; }
else if(strncmp(scanned[5], colors[6], 7) == 0) { rtc = 25; }
else if(strncmp(scanned[5], colors[8], 7) == 0) { rtc = 10; }
else if(strncmp(scanned[5], colors[9], 7) == 0) { rtc = 5; }
printf("Tempco = %d ppm/degreeC\n", rtc);
}
Error output: