i have a homework assignment in which i am supposed to find the nominal value of a fixed resistor, given the resistor's color code. each color code has a corresponding letter and i need to make a user defined function that converts that letter to a number. the numbers for the color codes are as follows:
0 - B(Black)
1 - N(Brown)
2 - R(Red)
3 - O(Orange)
4 - Y(Yellow)
5 - G(Green)
6 - L(Blue)
7 - V(Violet)
8 - A(Gray)
9 - W(White)
-1 - D(Gold)
-2 - S(Silver)

so far i have written this much code
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int dig1,dig2,dig3;
printf("These are the color choices you have: \nBlack-B\nBrown-N\nRed-R\nOrange-O\nYellow-Y\nGreen-G\nBlue-L\nViolet-V\nGray-A\nWhite-W\nGold-D\nSilver-S\n");
printf("Enter first color->");
scanf("%d",&dig1);
printf("Enter second color->");
scanf("%d",&dig2);
printf("Enter third color->");
scanf("%d",&dig3);
float
nom=((10*dig1)+dig2)*pow(10,dig3);
printf("The nominal resistance is %f",nom);
getch();
return 0;
}
i still need the user defined function to convert the color code letters to the numbers but i have no idea what to do! please help someone!

2. In your function, you can use if statements or a switch as shown below:

Using if statements:
Code:
int returncode(char color)
{
if (color=='B')
return 0;
if (color=='N')
return 1;
/*and so on*/
}
Using switch:
Code:
int returncode(char color)
{
switch(color)
{
case 'B':
return 0;
case 'N':
return 1;
/*and so on*/
}
}

3. well i put that in and i have this:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int returncode(char color);
int dig1,dig2,dig3;
printf("These are the color choices you have: \nBlack-B\nBrown-N\nRed-R\nOrange-O\nYellow-Y\nGreen-G\nBlue-L\nViolet-V\nGray-A\nWhite-W\nGold-D\nSilver-S\n");
printf("Enter first color->\n");
dig1=getch();
printf("Enter second color->\n");
dig2=getch();
printf("Enter third color->\n");
dig3=getch();
float
nom=((10*dig1)+dig2)*pow(10,dig3);
printf("The nominal resistance is %f",nom);
getch();
return 0;
}
int returncode(char color)
{
switch(color)
{
case 'B':
return 0;
case 'N':
return 1;
case 'R':
return 2;
case 'O':
return 3;
case 'Y':
return 4;
case 'G':
return 5;
case 'L':
return 6;
case 'V':
return 7;
case 'A':
return 8;
case 'W':
return 9;
case 'D':
return -1;
case 'S':
return -2;
}
}
my answers all come out as "1.#INF00" any idea?

4. dig1, dig2 and dig3 contain the character codes and should be converted into their respective integer values before being used in the equation:
Code:
nom=(10.0*returncode(dig1)+returncode(dig2))*pow(10.0,(double)returncode(dig3));
Also, note that getch() is not a standard C function, nor is conio.h a standard header file, so these are not portable.

5. so if I use this:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
int returncode(char color);
int dig1,dig2,dig3;
printf("These are the color choices you have: \nBlack-B\nBrown-N\nRed-R\nOrange-O\nYellow-Y\nGreen-G\nBlue-L\nViolet-V\nGray-A\nWhite-W\nGold-D\nSilver-S\n");
printf("Enter first color->");
scanf("%d",&dig1);
printf("Enter second color->");
scanf("%d",&dig2);
printf("Enter third color->");
scanf("%d",&dig3);
float
nom=(10.0*returncode(dig1)+returncode(dig2))*pow(10.0,(double)returncode(dig3));
printf("The nominal resistance is %f",nom);
getch();
return 0;
}
int returncode(char color)
{
switch(color)
{
case 'B':
return 0;
case 'N':
return 1;
case 'R':
return 2;
case 'O':
return 3;
case 'Y':
return 4;
case 'G':
return 5;
case 'L':
return 6;
case 'V':
return 7;
case 'A':
return 8;
case 'W':
return 9;
case 'D':
return -1;
case 'S':
return -2;
}
}
that gets rid of the portability problems but then after i enter the first color it prints everything after that

6. you are declaring the function returncode inside main. you cannot declare a function inside another function at all.

7. ohh yes i didnt notice that. so i changed it to:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int returncode(char color);
int main(){
int dig1,dig2,dig3;
printf("These are the color choices you have: \nBlack-B\nBrown-N\nRed-R\nOrange-O\nYellow-Y\nGreen-G\nBlue-L\nViolet-V\nGray-A\nWhite-W\nGold-D\nSilver-S\n");
printf("Enter first color->");
scanf("%d",&dig1);
printf("Enter second color->");
scanf("%d",&dig2);
printf("Enter third color->");
scanf("%d",&dig3);
float
nom=(10.0*returncode(dig1)+returncode(dig2))*pow(10.0,(double)returncode(dig3));
printf("The nominal resistance is %f",nom);
getch();
return 0;
}
int returncode(char color)
{
switch(color)
{
case 'B':
return 0;
case 'N':
return 1;
case 'R':
return 2;
case 'O':
return 3;
case 'Y':
return 4;
case 'G':
return 5;
case 'L':
return 6;
case 'V':
return 7;
case 'A':
return 8;
case 'W':
return 9;
case 'D':
return -1;
case 'S':
return -2;
}
}
this still doesnt solve my problem. when i enter the first color it automatically prints everything else after that

8. Avoid scanf, if you can, it's confusing you.

9. Don't use %d in scanf(), because you're scanning in characters, not digits. Use %c, and put a space before it so that whitespace left from the previous input is skipped.

Code:
scanf(" %c", &var);

10. sounds like the old buffer bull...........
http://faq.cprogramming.com/cgi-bin/...&id=1043284392

11. i was finally able to get it working correctly. i am extremely thankful to everyone who has helped me for this assignment! i would not have been able to finish this without you guys!! thanks alot!!

12. Originally Posted by Three
you are declaring the function returncode inside main. you cannot declare a function inside another function at all.
Why not?

13. Because.

14. I'm asking because K&R 2 has many such examples. For eg, see pages 72, 120, 125 ...

15. Okay, hasty reply. Declare or define? [Sorry, lacking K&R2.]

Popular pages Recent additions