Pelles C forum

C language => Beginner questions => Topic started by: ggmel on November 21, 2013, 12:14:35 AM

Title: Resistor color code problem
Post by: ggmel on November 21, 2013, 12:14:35 AM
Point me in the right direction please

Code: [Select]
#include <stdio.h>
#include <math.h>


int main (void)
{
char code1, code2, code3, code4;
double band, resistance;
double color1, color2, color3, color4;
double  decode_char( char code );


code1 = getchar();
code2 = getchar();
code3 = getchar();
code4 = getchar();


  color1 = decode_char( code1 );
     color2 = decode_char( code2 );
     color3 = decode_char( code3 );
color4 = decode_char( code4 );



     
         
     


printf("Is the resistor 3 or 4 band? ");
scanf("%lf", &band);

if(band == 3) {
printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
    printf( "COLOR\t\t\tCODE\n\t" );
    printf( "-----\t\t\t----\n\n" );
    printf( "\tBlack___________________________> N\n" );
    printf( "\tBrown___________________________> B\n" );
    printf( "\tRed_____________________________> R\n" );
    printf( "\tOrange__________________________> O\n" );
    printf( "\tYellow__________________________> Y\n" );
    printf( "\tGreen___________________________> G\n" );
    printf( "\tBlue____________________________> I\n" );
    printf( "\tViolet__________________________> V\n" );
    printf( "\tGrey____________________________> E\n" );
    printf( "\tWhite___________________________> W\n" );
printf( "\n\n\tEnter three colors with no spaces. ");


resistance = ( 10.0 * color1  +  color2 )
                       * pow( 10.0, color3 );
printf("\n\n Resistance in ohms: %f", resistance);
}

if(band == 4) {

printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
    printf( "COLOR\t\t\t\tCODE\n\t" );
    printf( "-----\t\t\t\t----\n\n" );
    printf( "\tBlack___________________________> N\n" );
    printf( "\tBrown___________________________> B\n" );
    printf( "\tRed_____________________________> R\n" );
    printf( "\tOrange__________________________> O\n" );
    printf( "\tYellow__________________________> Y\n" );
    printf( "\tGreen___________________________> G\n" );
    printf( "\tBlue____________________________> I\n" );
    printf( "\tViolet__________________________> V\n" );
    printf( "\tGrey____________________________> E\n" );
    printf( "\tWhite___________________________> W\n" );
printf( "\n\n\tEnter four colors with no spaces. ");



resistance = ( 10.0 * color1 + color2 + color3 )
                       * pow( 10.0, color4 );
printf("\n\n Resistance in ohms: %f", resistance);
}


return (0);
}



3


     switch ( code ) {
     case 'N':
          return 0.0;
     case 'B':
          return 1.0;
     case 'R':
          return 2.0;
     case 'O':
          return 3.0;
     case 'Y':
          return 4.0;
     case 'G':
          return 5.0;
     case 'I':
          return 6.0;
     case 'V':
          return 7.0;
     case 'E':
          return 8.0;
     case 'W':
          return 9.0;
}

}



with this program i get one error
(88): warning #2235: Not all control paths return a value.
Title: Re: Resistor color code problem
Post by: Stefan Pendl on November 21, 2013, 12:42:46 AM
The last switch condition is hanging in mid air.

Make sure to include all code inside the main function, if you don't need it comment it out, but never move it outside the function.
Title: Re: Resistor color code problem
Post by: ggmel on November 21, 2013, 01:12:15 AM
When I do include it i get error #2048: Undeclared identifier 'code.


Code: [Select]
#include <stdio.h>
#include <math.h>

int main (void)
{
char code1, code2, code3, code4;
double band, resistance;
double color1, color2, color3, color4;
double  decode_char( char code );


code1 = getchar();
code2 = getchar();
code3 = getchar();
code4 = getchar();


  color1 = decode_char( code1 );
     color2 = decode_char( code2 );
     color3 = decode_char( code3 );
color4 = decode_char( code4 );



     
         
     


printf("Is the resistor 3 or 4 band? ");
scanf("%lf", &band);

if(band == 3) {
printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
    printf( "COLOR\t\t\tCODE\n\t" );
    printf( "-----\t\t\t----\n\n" );
    printf( "\tBlack___________________________> N\n" );
    printf( "\tBrown___________________________> B\n" );
    printf( "\tRed_____________________________> R\n" );
    printf( "\tOrange__________________________> O\n" );
    printf( "\tYellow__________________________> Y\n" );
    printf( "\tGreen___________________________> G\n" );
    printf( "\tBlue____________________________> I\n" );
    printf( "\tViolet__________________________> V\n" );
    printf( "\tGrey____________________________> E\n" );
    printf( "\tWhite___________________________> W\n" );
printf( "\n\n\tEnter three colors with no spaces. ");


resistance = ( 10.0 * color1  +  color2 )
                       * pow( 10.0, color3 );
printf("\n\n Resistance in ohms: %f", resistance);
}

if(band == 4) {

printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
    printf( "COLOR\t\t\t\tCODE\n\t" );
    printf( "-----\t\t\t\t----\n\n" );
    printf( "\tBlack___________________________> N\n" );
    printf( "\tBrown___________________________> B\n" );
    printf( "\tRed_____________________________> R\n" );
    printf( "\tOrange__________________________> O\n" );
    printf( "\tYellow__________________________> Y\n" );
    printf( "\tGreen___________________________> G\n" );
    printf( "\tBlue____________________________> I\n" );
    printf( "\tViolet__________________________> V\n" );
    printf( "\tGrey____________________________> E\n" );
    printf( "\tWhite___________________________> W\n" );
printf( "\n\n\tEnter four colors with no spaces. ");


resistance = ( 10.0 * color1 + color2 + color3 )
                       * pow( 10.0, color4 );
printf("\n\n Resistance in ohms: %f", resistance);
}


return (0);




double decode_char( char code );{

     switch(code) {
     case 'N':
          return 0.0;
     case 'B':
          return 1.0;
     case 'R':
          return 2.0;
     case 'O':
          return 3.0;
     case 'Y':
          return 4.0;
     case 'G':
          return 5.0;
     case 'I':
          return 6.0;
     case 'V':
          return 7.0;
     case 'E':
          return 8.0;
     case 'W':
          return 9.0;
}

}
}



Title: Re: Resistor color code problem
Post by: jj2007 on November 21, 2013, 02:53:19 AM
Try this - slightly rearranged...
Code: [Select]
#include <stdio.h>
#include <math.h>

#pragma comment(linker, "/SubSystem:Console")
#pragma warn(disable:2216)    // retval never used
#pragma warn(disable:2215)    // conversion ... loss of data

double decode_char( char code ) {
     switch (code) {
     case 'N':
          return 0.0;
     case 'B':
          return 1.0;
     case 'R':
          return 2.0;
     case 'O':
          return 3.0;
     case 'Y':
          return 4.0;
     case 'G':
          return 5.0;
     case 'I':
          return 6.0;
     case 'V':
          return 7.0;
     case 'E':
          return 8.0;
     case 'W':
          return 9.0;
}
}

int main (void)
{
  char code1, code2, code3, code4;
  double band, resistance;
  double color1, color2, color3, color4;
  code1 = getchar();
  code2 = getchar();
  code3 = getchar();
  code4 = getchar();
  color1 = decode_char( code1 );
  color2 = decode_char( code2 );
  color3 = decode_char( code3 );
  color4 = decode_char( code4 );

  printf("Is the resistor 3 or 4 band? ");
  scanf("%lf", &band);

   if(band == 3) {
      printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
         printf( "COLOR\t\t\tCODE\n\t" );
         printf( "-----\t\t\t----\n\n" );
         printf( "\tBlack___________________________> N\n" );
         printf( "\tBrown___________________________> B\n" );
         printf( "\tRed_____________________________> R\n" );
         printf( "\tOrange__________________________> O\n" );
         printf( "\tYellow__________________________> Y\n" );
         printf( "\tGreen___________________________> G\n" );
         printf( "\tBlue____________________________> I\n" );
         printf( "\tViolet__________________________> V\n" );
         printf( "\tGrey____________________________> E\n" );
         printf( "\tWhite___________________________> W\n" );
      printf( "\n\n\tEnter three colors with no spaces. ");
     

      resistance = ( 10.0 * color1  +  color2 )
                       * pow( 10.0, color3 );
      printf("\n\n Resistance in ohms: %f", resistance);
   }

    if(band == 4) {
     
      printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
         printf( "COLOR\t\t\t\tCODE\n\t" );
         printf( "-----\t\t\t\t----\n\n" );
         printf( "\tBlack___________________________> N\n" );
         printf( "\tBrown___________________________> B\n" );
         printf( "\tRed_____________________________> R\n" );
         printf( "\tOrange__________________________> O\n" );
         printf( "\tYellow__________________________> Y\n" );
         printf( "\tGreen___________________________> G\n" );
         printf( "\tBlue____________________________> I\n" );
         printf( "\tViolet__________________________> V\n" );
         printf( "\tGrey____________________________> E\n" );
         printf( "\tWhite___________________________> W\n" );
      printf( "\n\n\tEnter four colors with no spaces. ");

      resistance = ( 10.0 * color1 + color2 + color3 )
                       * pow( 10.0, color4 );
      printf("\n\n Resistance in ohms: %f", resistance);
   }

   return (0);
   
   }
Title: Re: Resistor color code problem
Post by: ggmel on November 21, 2013, 03:18:47 AM
With that I am unable to enter and colors for the resistors.  When i put in a scanf i get: resistance is nan.
Title: Re: Resistor color code problem
Post by: jj2007 on November 21, 2013, 03:30:40 AM
I didn't check the logic of your prog, I just made it compile without errors.

Regarding inputs: You have four getchar() statements, followed by a scanf. Check at least the documentation for getchar().

Perhaps you should move the scanf above the getchar() stuff:
  printf("Is the resistor 3 or 4 band? ");
  scanf("%lf", &band);
// INSERT HERE the print 3/4 colours with no spaces statement
  code1 = getchar();
  code2 = getchar();
  code3 = getchar();
  code4 = getchar();


Note also that the Case statements expect an uppercase letter.

Working code attached, in C and Assembler.
Title: Re: Resistor color code problem
Post by: Bitbeisser on November 21, 2013, 05:15:17 AM
I didn't check the logic of your prog, I just made it compile without errors.
Well, no, you just cheated....
Quote
Working code attached, in C and Assembler.
off-topic...  ;)

Ralf
Title: Re: Resistor color code problem
Post by: jj2007 on November 21, 2013, 07:38:12 AM
off-topic...  ;)

It is, it is - just a demo how the OP's task could be achieved better. Sorry that you don't like the language, but it's the same that's working under the hood of Pelles C ;-)

Otherwise, how's life in Germany?
Title: Re: Resistor color code problem
Post by: ggmel on November 21, 2013, 11:50:36 AM
Thanks for the help!
Title: Re: Resistor color code problem
Post by: TimoVJL on November 21, 2013, 04:09:20 PM
Handle silver and gold too.
Correct that four color function.
After reading 4 codes, you can check from code4, if there is four bands.

0.22 ohm RED RED SILVER
color codes (http://en.wikipedia.org/wiki/Electronic_color_code)
Title: Re: Resistor color code problem
Post by: Bitbeisser on November 22, 2013, 01:35:14 AM
off-topic...  ;)

It is, it is - just a demo how the OP's task could be achieved better. Sorry that you don't like the language, but it's the same that's working under the hood of Pelles C ;-)
Well, I am using assembler for pretty much 37 years now, and can't say that I don't like it, I just use it where appropriate.

And the whole program can be properly programmed in C, the OP just need to learn/understand how to structure a program. His problems are all (mostly, beside some general C issues) due to trying to cramp everything into the main () function, that's not how you program in C...
Quote
Otherwise, how's life in Germany?
Don't know, haven't been there in a long time...  ;)

Ralf