Point me in the right direction please
#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.
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.
When I do include it i get error #2048: Undeclared identifier 'code.
#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;
}
}
}
Try this - slightly rearranged...
#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);
}
With that I am unable to enter and colors for the resistors. When i put in a scanf i get: resistance is nan.
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.
Quote from: 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.
Well, no, you just cheated....
QuoteWorking code attached, in C and Assembler.
off-topic... ;)
Ralf
Quote from: Bitbeisser on November 21, 2013, 05:15:17 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?
Thanks for the help!
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)
Quote from: jj2007 on November 21, 2013, 07:38:12 AM
Quote from: Bitbeisser on November 21, 2013, 05:15:17 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...
QuoteOtherwise, how's life in Germany?
Don't know, haven't been there in a long time... ;)
Ralf