Hey,
I'm coding a program that can take input of a resistors 3,4 or 5 color bands. The program should then decode the color bands and give you the resistance value.
I've coded the program but I have a logic error somewhere that I cannot find...
Because its a logic error there aren't any warnings, bugs, syntax errors. I thought I should be able to compile it and run it using system("pause") or Printf("f") statements to tell me how far my program makes it before it quits. Now I can' t even get the program to run at all before it crashes. I can't really dissect the program and try to run parts because lots of the variables span across a couple functions.
I'm wondering what some good troubleshooting techniques are....
Ill post the code if anybody wants to be amazing and help. Otherwise any tips and suggestions are much appreciated...
There are 2 pretty big switch statements that make this program over 200 lines, I'm going to omit them for the sake of the rest of the program being the probably area my mistake is...
Thanks!
Code:
#include <stdio.h>
#include <stdlib.h>
void getColorCode(char colorCode[], int band_num); //prototype for function that inputs color chars into array
double getResistorValue( char colorCode[], int colorValue[], int r_value, int band_num ); //prototype for function that finds resistor value
int getmultiplier (int band_num, char colorCode[], double multiplier); //prototype for function within getresistorvalue that determines multiplier from color band array
int main (void)
{
double r_value = 0; //variable for resistor value
int band_num; //variable for number of bands on resistor
char colorCode[band_num]; //array declaration for color band selection from user to be stored
int colorValue[band_num]; //array declaration for resistors after they've been converted to numbers in getresistorvalue function
int while_switch = 0; //controls the while loop that repeats the program or exits program in main()
char repeater; // Stores y or n answer from user then is used to change while_switch variable to exit program
printf("This program will calculate the value of a resistor.\n\n");
while (while_switch != 1){
printf("Does your resistor have 3,4 or 5 bands?"); //user output and input for how many color bands resistor has
scanf("%d",&band_num);
band_num -=1; //-1 so when used for array positioning will account for 0 and not have an extra variable
printf("B = Black\nN = Brown\nR= Red\nO = Orange\nY = Yellow\n");
printf("G = Green\nL = Blue\nV = Violet\nE = Gray\nW = White\n\n P = Gold \n\n H - Silver\n");
printf("Enter the %d-character color combination of the resistor: ",band_num);
getColorCode(colorCode, band_num); //function call for user to enter values into array
r_value = getResistorValue(colorCode, colorValue, r_value, band_num); //function call to calculate resistance
printf("\n\nThis resistors value is %lf Ohms.\n", r_value);
printf("Would you like to find another resistor value?\n Enter y or n:\n"); //user output to repeat program
fflush(stdin);
scanf("%c" ,&repeater); //coverts users input to control while_switch
if(repeater == 'y'){
while_switch = 0;
}
else if (repeater == 'n'){
while_switch = 1;
}}
system("pause");
return 0;
}
void getColorCode(char colorCode[], int band_num) //function that takes color band selection and stores it into array
{
int i;
for(i = 0; i < band_num; i++)
colorCode[i] = getchar();
}
double getResistorValue( char colorCode[], int colorValue[], int r_value, int band_num ){ //function that calculates resistance
int i; //loop counter
double multiplier = 0; //value returned from getmultiplier function for calculation at end of function
for( i = 0; i < (band_num-1); i++) //color to number conversion of resistance. Loops as many times as value of band_num
{
switch (colorCode[i])
{
**REMOVED SWITCH STATEMENT**
}}
getmultiplier(band_num, colorCode, multiplier); //function call to program that determines multiplier from bands
r_value = ((colorValue[0] + (colorValue[1]) + (colorValue[2])) * multiplier); //final resistance calculation stored to variable r_value
return r_value;
}
int getmultiplier (int band_num, char colorCode[], double multiplier){ //function call that changes multiplier from a color to a number
int j;
int mult_band;
switch (band_num) //switch statement to determine which band is the multiplier band
{
case(3):
case(4):
mult_band = 3-1;
break;
case(5):
mult_band = 4-1;
break;
default:
break;
}
switch (colorCode[mult_band]) //switch statement that converts multiplier band from color to number
{
**REMOVED SWITCH STATEMENT**
}
return multiplier; //returns number to getresistor value for final resistor calculation
}