Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-26-2006
alexwink
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!
• 10-26-2006
noodles
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*/     } }```
• 10-26-2006
alexwink
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?
• 10-26-2006
noodles
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.
• 10-26-2006
alexwink
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
• 10-26-2006
Three
you are declaring the function returncode inside main. you cannot declare a function inside another function at all.
• 10-26-2006
alexwink
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
• 10-26-2006
Dave_Sinkula
Avoid scanf, if you can, it's confusing you.
• 10-26-2006
noodles
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-26-2006
Three
sounds like the old buffer bull...........
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
• 10-26-2006
alexwink
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!!
• 10-26-2006
noodles
Quote:

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

Why not?
• 10-26-2006
Dave_Sinkula
Because.
• 10-27-2006
noodles
I'm asking because K&R 2 has many such examples. For eg, see pages 72, 120, 125 ...
• 10-27-2006
Dave_Sinkula
Okay, hasty reply. Declare or define? [Sorry, lacking K&R2.]
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last