homework problem Please Help!

This is a discussion on homework problem Please Help! within the C Programming forums, part of the General Programming Boards category; i have a homework assignment in which i am supposed to find the nominal value of a fixed resistor, given ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    homework problem Please Help!

    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. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    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. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    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. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    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. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    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. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    you are declaring the function returncode inside main. you cannot declare a function inside another function at all.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    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. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Avoid scanf, if you can, it's confusing you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    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. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    sounds like the old buffer bull...........
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    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. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    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?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Because.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    I'm asking because K&R 2 has many such examples. For eg, see pages 72, 120, 125 ...

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Okay, hasty reply. Declare or define? [Sorry, lacking K&R2.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 05:39 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM

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