Like Tree1Likes
  • 1 Post By CommonTater

Help with a resistor decoder

This is a discussion on Help with a resistor decoder within the C Programming forums, part of the General Programming Boards category; I am currently working a program that is supposed to decode a six banded resistor, but every time I run ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    19

    Help with a resistor decoder

    I am currently working a program that is supposed to decode a six banded resistor, but every time I run the program it tells me that I have entered an invalid color, and the compiler also gives me a warning that I have parameter names (without types) in function declaration.

    Here is the code. Any help would be greatly appreciated.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdbool.h>
    #include<string.h>
    bool color_check(char (*col)[7], const char (*r_col)[7]);
    void get_resistance(char (*col)[7], const char (*r_col)[7]);
    void get_tolerance(char (*col)[7], const char (*r_col)[7]);
    void get_tempco(char (*col)[7], const char (*r_col)[7]);
    char ch;
    
    int main(void)
    {
    do{
        char col[6][7];
        const char r_col[12][7]={"silver","gold","black","brown","red","orange","yellow","green","blue","violet","grey","white"};
        
        input:
        printf("\t\t*****Resistor Calculator*****\n");
        printf("Please enter the color of the six bands seperated by a space.\n");
        scanf("%s %s %s %s %s %s", &col[0], &col[1], &col[2], &col[3], &col[4], &col[5]);
        
        printf("%s %s %s %s %s %s", col[0], col[1], col[2], col[3], col[4], col[5]);
        
        if(color_check(col, r_col))
        {
          void get_resistance(col, r_col);
          void get_tolerance(col, r_col);
          void get_tempco(col, r_col);
        }
        else
        {
          printf("You have entered an invalid color. Please try again");
          goto input;
        }
    
    
    }while(ch == 'Y' || ch == 'y');
        return 0;
    }
    
    bool color_check(char (*col)[7], const char (*r_col)[7])
    {
      int i, j;
      
      for(i = 0; i < 12; i++)
      {
            for(j = 0; j < 7; j++)
            {
                  if(strncmp(col[j], r_col[i], 7) != 0)
                  {
                  return false;
                  }
            }
      }
      
        if((strncmp(col[0], r_col[2], 7) == 0)||(strncmp(col[0], r_col[1], 7) == 0)||(strncmp(col[0], r_col[0], 7) == 0))
        { 
           return false; 
        }
        else if((strncmp(col[1], r_col[1], 7) == 0)||(strncmp(col[1], r_col[0], 7) == 0)||(strncmp(col[2], r_col[1], 7) == 0)||(strncmp(col[2], r_col[0], 7) == 0))
        {
           return false; 
        }
        else if(strncmp(col[4], r_col[11], 7) == 0) 
        { 
           return false; 
        }
        else if((strncmp(col[5], r_col[7], 7) == 0)||(strncmp(col[5], r_col[10], 7) == 0)||(strncmp(col[5], r_col[11], 7) == 0)||(strncmp(col[5], r_col[1], 7) == 0)||(strncmp(col[5], r_col[0], 7) == 0))
        { 
           return false; 
        }
        else
        {
           return true;
        }       
    }
    
    void get_resistance(char (*col)[7], const char (*r_col)[7])
    {
        int i;
        double color_pts, resist, exp;
        
        for(i=0; i<4; i++)
        {
          if (strncmp(col[i], r_col[0], 7) == 0 ) 
          { 
            color_pts = -2; 
            exp = .01; 
          }
           else if (strncmp(col[i], r_col[1], 7) == 0 )
           { 
             color_pts = -1;
             exp = .1;
           }
           else if (strncmp(col[i], r_col[2], 7) == 0 ) 
           { 
             color_pts = 0;
             exp = 1;
           }
           else if (strncmp(col[i], r_col[3], 7) == 0 )
           { 
             color_pts = 1;
             exp = 10;
           }
           else if (strncmp(col[i], r_col[4], 7) == 0 )
           { 
             color_pts = 2;
             exp = 100;
           } 
           else if (strncmp(col[i], r_col[5], 7) == 0 )
           { 
             color_pts= 3;
             exp = 1000;
           }
           else if (strncmp(col[i], r_col[6], 7) == 0 ) 
           {
             color_pts;
             exp = 10000;
           }
           else if (strncmp(col[i], r_col[7], 7) == 0 )
           { 
             color_pts = 5;
             exp = 100000;
           }
           else if (strncmp(col[i], r_col[8], 7) == 0 ) 
           {
             color_pts = 6;
             exp = 1000000;
           }
           else if (strncmp(col[i], r_col[9], 7) == 0 ) 
           { 
             color_pts = 7;
             exp = 10000000;
           }
           else if (strncmp(col[i], r_col[10], 7) == 0 )
           {
             color_pts = 8;
             exp = 100000000;
           }
           else if (strncmp(col[i], r_col[11], 7) == 0 )
           { 
             color_pts = 9;
             exp = 1000000000;
           }
           
           if(i == 0)
           { 
             resist = color_pts * 100; 
           } 
           if(i == 1)
           { 
             resist += color_pts * 10;
           } 
           if(i == 2)
           { 
             resist += color_pts;
           }
           if(i == 3)
           {
             resist = color_pts * exp;
           }
        }   
    
      if(resist < 1000000)
      { 
        printf("Resistance = %.2f kOhms\n", resist/1000);
      }
      else 
      { 
        printf("Resistance = %.2f MOhms\n", resist/1000000); 
      }
    }
    
    void get_tolerance(char (*col)[7], const char (*r_col)[7])
    {
         double tolerance;
        
        if(strncmp(col[4], r_col[3], 7) == 0)
        { 
          tolerance = 1; 
        }
        else if(strncmp(col[4], r_col[4], 7) == 0)
        {
          tolerance = 2;
        }
        else if(strncmp(col[4], r_col[5], 7) == 0)
        { 
          tolerance = 3;   
        }
        else if(strncmp(col[4], r_col[6], 7) == 0)
        { 
          tolerance = 4; 
        }
        else if(strncmp(col[4], r_col[7], 7) == 0)
        {
          tolerance = .5; 
        }
        else if(strncmp(col[4], r_col[8], 7) == 0)
        {
          tolerance = .25; 
        }
        else if(strncmp(col[4], r_col[9], 7) == 0)
        {
          tolerance = .1;
        }
        else if(strncmp(col[4], r_col[10],7) == 0)
        {
          tolerance = .05; 
        }
        else if(strncmp(col[4], r_col[1], 7) == 0)
        { 
          tolerance = 5;   
        }
        else if(strncmp(col[4], r_col[0], 7) == 0)
        { 
          tolerance = 10; 
        }
         
        printf("Tolerance = +/- %.2g%%\n", tolerance);
    }
    
    void get_tempco(char (*col)[7], const char (*r_col)[7])
    {
         int tempco;
         
         if(strncmp(col[5], r_col[3], 7) == 0)
         {
           tempco = 100;
         }
         else if(strncmp(col[5], r_col[4], 7) == 0)
         {
           tempco = 50;
         }
         else if(strncmp(col[5], r_col[5], 7) == 0)
         {
           tempco = 15;
         }
         else if(strncmp(col[5], r_col[6], 7) == 0)\
         { 
           tempco = 25;
         }
         else if(strncmp(col[5], r_col[8], 7) == 0)
         {
           tempco = 10;
         }
         else if(strncmp(col[5], r_col[9], 7) == 0)
         {
           tempco = 5;
         }     
         printf("Tempco = %d ppm/degreeC\n", tempco);
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Syntax error:

    Code:
        if(color_check(col, r_col))
        {
          void get_resistance(col, r_col);
          void get_tolerance(col, r_col);
          void get_tempco(col, r_col);
        }
        else
        {
          printf("You have entered an invalid color. Please try again");
          goto input;
    When you call a function you shouldn't prefix the call with the return type. You shouldn't have "void" in there.


    The colour checking code....
    Code:
      for(i = 0; i < 12; i++)
      {
            for(j = 0; j < 7; j++)
            {
                  if(strncmp(col[j], r_col[i], 7) != 0)
                  {
                  return false;
                  }
            }
      }
    If I am reading this right, you're trying to just confirm that the input was actually a valid colour string. This doesn't work. For example, say the first colour I put in was "white". Then r_col[i] is "silver" the first time through the loop. The strncmp fails and the whole function returns false.

    You need to only return false if a colour is really invalid. You could do this by having having an extra variable to record whether or not the colour is valid.

    I think this should do it:
    Code:
        bool col_valid = true;
    
        for(j = 0; j < 7; j++)
    	{
    		if (!col_valid)
    			return false;
    		col_valid = false;
    		for(i = 0; i < 12; i++)
    		{
    			if(strncmp(col[j], r_col[i], 7) == 0)
    			{
    				col_valid = true;
    				break;
    			}
    		}
    	}

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    oh - I didn't get any warnings about missing types in declarations. Different compiler versions I guess.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also learn about loops... goto is a leftover from the old days of BASIC that has little or no real use in modern programming.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by smokeyangel View Post
    oh - I didn't get any warnings about missing types in declarations. Different compiler versions I guess.
    Turn the warning level on your compiler up to maximum... and treat all warnings as errors to be fixed.
    AndrewHunter likes this.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    Cool, it validates colors correctly now. CommonTater, you are a boss. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resistor Color Decoder for a 6 band resistor
    By nivoca in forum C Programming
    Replies: 2
    Last Post: 06-25-2011, 01:44 PM
  2. Mpeg2 decoder
    By thangdc01_02 in forum C Programming
    Replies: 6
    Last Post: 11-21-2010, 04:09 PM
  3. decoder
    By jalenamichelle in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2010, 07:24 AM
  4. GIF Decoder (SZW Compression)
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 05-15-2008, 02:48 AM
  5. MP3 Decoder
    By Pete in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-02-2005, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21