Thread: Logic error in larger program, troubleshoot tips/help

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    17

    Logic error in larger program, troubleshoot tips/help

    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
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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
    These arrays don't magically resize themselves whenever you enter a value for band_num

    They're set to whatever the garbage value in band_num is at that point, and if that random value is negative, you lose!

    If 5 is the max, then make then [5]

    > fflush(stdin);
    > scanf("%c" ,&repeater); //coverts users input to control while_switch
    fflush isn't defined for standard input.
    You can get the same effect by simply using " %c" as the format, if all you want to do is skip a newline.

    > I'm wondering what some good troubleshooting techniques are....
    It's called a debugger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    17
    Quote Originally Posted by Salem View Post
    > 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
    These arrays don't magically resize themselves whenever you enter a value for band_num

    They're set to whatever the garbage value in band_num is at that point, and if that random value is negative, you lose!

    If 5 is the max, then make then [5]

    > fflush(stdin);
    > scanf("%c" ,&repeater); //coverts users input to control while_switch
    fflush isn't defined for standard input.
    You can get the same effect by simply using " %c" as the format, if all you want to do is skip a newline.

    > I'm wondering what some good troubleshooting techniques are....
    It's called a debugger.

    Hey, thanks for the reply.
    It has to be a variable though because it has to be prepared to take 3,4 or 5. are you saying just make it a defined amount to start off with? then when the user enters it change it?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No, you can store 3 elements in an array of 5 elements.

    Just so long as you know there are only 3, then all is fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    17

    Thumbs up

    Okay, That makes sense. Thanks.

    I've work past a few other minor kinks...
    the code gets to the getColorCode function
    the printout prints:

    Print number 1
    print number 2

    it doesn't stop and do my getchar call.
    sometimes it works sometimes it skips.
    is that due to the input buffer?
    is this the correct usage of fflush? or is it something else?

    I changed it a bit from above, it now looks like this so it reads out what variable the user is supposed to enter:

    Code:
    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++)
        {
            printf("\n Enter band number %d \n",(i+1));
    
    
            colorCode[i] = getchar();
        }}

    I feel like it should work because previously in main I decrement band_num so it is an accurate variable considering arrays start at 0.

    If the loop lets me enter in more then one number the readout is accurate. I just have to get passed this last hitch.

    Thanks for all the help so far, you guys can't fathom how helpful and patient you are... maybe you can..




    **edit: on second thought, if i enter getchar() into the array that is already declared in main, passed to this function, would this function be void? or would I have to return the values to pass them on? Since its an array you can only pass 1 thing through return, meaning I'd have to use pointers?
    Last edited by Glassjaw; 12-05-2015 at 03:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program hangs when larger input data
    By 花颂 in forum C Programming
    Replies: 1
    Last Post: 04-03-2013, 09:55 PM
  2. A logic error in my program
    By 20120903 in forum C Programming
    Replies: 5
    Last Post: 09-06-2012, 04:00 AM
  3. Error when trying to read and process larger files
    By tanjinjack in forum C Programming
    Replies: 19
    Last Post: 03-25-2011, 12:15 PM
  4. Troubleshoot Please
    By xinglong in forum C Programming
    Replies: 4
    Last Post: 03-03-2010, 10:51 AM
  5. C Program logic error?
    By bigmac(rexdale) in forum C Programming
    Replies: 27
    Last Post: 02-15-2008, 11:20 AM