Thread: Working program, seeking advice on cleaning it up.(resistor decode) *Newbie

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Boise Idaho
    Posts
    4

    Working program, seeking advice on cleaning it up.(resistor decode) *Newbie

    Obviously I am new to programming, and I realize that there is most likely better ways to go about what I did here. Seeking advice.

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include<ctype.h>
    
    
    void intro (void);
    double getCode(char code);
    double engVal(double );
    void cont(void);
    int main (void)
    {    
        char c1,c2,c3; 
        double num1, num2, num3, ohm;
        intro (); 
        printf("\n\n\tEnter your three band code\t=> ");
        scanf("%c %c %c", &c1, &c2, &c3);
        num1 = getCode(c1);    /*  Runs the 3 user inputs (c1, c2, c3) through the */
        num2 = getCode(c2);    /*   function "getCode(char code);" and brings back*/
        num3 = getCode(c3);    /*    an interger value. */
        _clrscr(); 
            if ( num1 == -999.0  ||  num2 == -999.0  ||  num3 == -999.0 )        /* Checks that the number is valid. */    
                printf( "\n\n\tBad code -- cannot compute resistance\n" ); 
        else {                                                                                        
        ohm = ( 10 * num1 + num2 ) * pow(10, num3);}   
       ///////////////////////////////////////////////////////////////////////////////////////////////////////////       Part of main that provides
        c1 = toupper(c1);c2 = toupper(c2);c3 = toupper(c3);     ////        the user the code that they typed
        printf("\n\n\t\t    *********");                                                   ////        in upper case (easier on the eyes) 
        printf("\n\tColor code  *  %c%c%c  *",c1,c2,c3);             ////        along with the value that the resistor 
        printf("\n\t\t    *********\n\t\t\t");                                            ////        code returned in engineering notation.
        ohm = engVal(ohm);                                                              ////
        printf(" ohms\n\n\n");                                       ////
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////
        cont();
        return (0);
    }   /* An introduction function giving direction for the user input*/
    void intro (void){
           printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
           printf( "\t COLOR\t\t  CODE\n\t" );
              printf( "\t---------\t----------\n" );
           printf( "\t\t| Black |\t| B or b |\n" );
           printf( "\t\t| Brown |\t| N or n |\n" );
           printf( "\t\t|   Red |\t| R or r |\n" );
           printf( "\t\t|Orange |\t| O or o |\n" );
           printf( "\t\t|Yellow |\t| Y or y |\n" );
           printf( "\t\t| Green |\t| G or g |\n" );
           printf( "\t\t|  Blue |\t| E or e |\n" );
           printf( "\t\t|Violet |\t| V or v |\n" );
           printf( "\t\t|  Gray |\t| A or a |\n" );
           printf( "\t\t| White |\t| W or w |\n" );
           printf( "\t\t---------\t----------\n" );
    }  /* Function to convert the input color code to a numeric value*/
    double getCode(char code){
            switch  (code ) {
                 case 'B': case'b': return 0.0; 
            case 'N': case'n': return 1.0; 
            case 'R': case'r': return 2.0; 
            case 'O': case'o': return 3.0;
            case 'Y': case'y':  return 4.0; 
            case 'G': case'g': return 5.0;     
            case 'E': case'e': return 6.0;        
            case 'V': case'v': return 7.0;
            case 'A': case'a': return 8.0; 
            case 'W': case'w': return 9.0;             
        default:
        return 0;
        }    
    }    /* Function to convert resistor value to engineering notation*/
    double engVal(double val){    
        if (val >= 1.0&&val < 1.0e3){        
            (val); printf("= :\t%.1f ", val);}                                             
        if (val >= 1e3&& val < 1e6){                       
            (val = val / 1e3); printf("\t= %.1f k", val);}                                                            
        if (val >=1e6&&val<1e9){                   
            (val = val / 1e6 ); printf("\t= %.1f M", val);}     
        if(val >=1e9&&val<1e12){
            (val = val / 1e9); printf("\t= %.1f G", val);} 
        if(val >=1e12&&val<1e15){
            (val = val / 1e12); printf("\t= %.1f T", val);} 
         if(val < 0.9&&val>=1e-3){
            (val = val *1e3); printf("\t= %.1f m", val);}
        if (val < 1e-3&&val>=1e-6){
            (val = val *1e6 ); printf("\t= %.1f u", val);}
        if (val < 1e-6&&val>=1e-9){
            (val = val *1e9); printf("\t= %.1f p", val);}
        if (val < 1e-9&&val>=1e-12){
            (val = val *1e12); printf("\t= %.1f p", val);}
    return(0);
    }     /* Continue function, gives the option to abort or enter another code*/
    void cont(void){
         int another;
        printf("\n\tEnter another code?\n\n\t");
        printf("Type 1 to continue,\n\t");
           printf(" or any key to abort\n\n\t");  
        another = 0;                     
        scanf("%d",&another);                                                         
        _clrscr();  
        if ( another == 1)        
    {    fflush ( stdin ) ;    
        main();    
        }else if ( another !=1 );                                    
        exit(1);
    }
    Also am wanting to make it more universal, possibly accept 4,5,6 band codes as well.
    Just having fun with it, its not a class project or anything like that but still would appreciate
    any input.
    Thanks!
    Last edited by Mick R. Groff; 01-16-2012 at 03:24 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're not supposed to call main recursively as you do in your cont function. Instead, put a loop in main to go back to the top.

    I'm assuming your spacing got messed up when you posted your code. If not, and you actually meant to put the comment describing a function on the same line as the terminating brace for the preceding function, then don't do that!

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    double getCode(char code){
            switch  (code ) {
                 case 'B': case'b': return 0.0;
            case 'N': case'n': return 1.0;
            case 'R': case'r': return 2.0;
            case 'O': case'o': return 3.0;
            case 'Y': case'y':  return 4.0;
            case 'G': case'g': return 5.0;    
            case 'E': case'e': return 6.0;       
            case 'V': case'v': return 7.0;
            case 'A': case'a': return 8.0;
            case 'W': case'w': return 9.0;            
        default:
        return 0;
        }   
    }
    You use toupper later in main(). Use it before you call getCode() to cut your number of cases in half.

    Code:
    fflush ( stdin ) ;
    This is results in undefined behavior and should not be used. See the FAQ.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It's standard to have a program exit with 0 upon success, not 1.

    Get rid of conio.h, and all of the _clrscr() commands. They're not portable, nor are they useful.

    Line 102, the else statement does nothing.

    Line 72 is probably a typo, because (val); does nothing.

    If an invalid character is inputted, nothing is done to handle it (it just parses the correct ones and then exits during the loop).

    The loop doesn't always work, it sometimes randomly decides to not display a character in the string, and then exits. This might have to do with the fflush(stdin), which you should remove.

    You should fix the recursion to main by using a separate function and a while loop.

    You can replace all of the scanf trickery in cont() with a simple getchar().

    On line 18, the scanf is very format specific, and screws the program over if the user doesn't follow the (somewhat unintuitive) input syntax. Instead, use a fgets() and then parse the string dynamically, skipping the spaces and extracting any valid characters.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    Boise Idaho
    Posts
    4
    Thanks to everyone that has given advice, like I said completely new to programming (week 3). I apparently know just enough to make some ugly code, but I enjoy it..
    I will work on implementing some things and will repost ...

    thanks again

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Boise Idaho
    Posts
    4
    I got rid of the "fflush ( stdin ) ;" and when you attempt to enter another code the value will not come out correctly. Is there another way to clear what ever is causing the incorrect output?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mick R. Groff View Post
    I got rid of the "fflush ( stdin ) ;" and when you attempt to enter another code the value will not come out correctly. Is there another way to clear what ever is causing the incorrect output?
    Yes... on any compiler that is either C99 or C89 compliant all you have to do is add a leading space in your scanf() family statements...
    Code:
    scanf(" %c...
    scanf(" %s...
    And it will ignore crap left in the input buffer

    Also looking at your source in message#1 ...
    1) It's ok to leave blank lines between functions or blocks of code.

    2) Putting comments on the line with the closing brace of a function makes your code ridiculously hard to read.

    3) Your engval() function is silly... A good function does one thing well... not 20 things haphazardly.

    4) Why define entval() to return a double when the only thing you return is a 0? Have the function calculate the resistence value and return the actual value. Do the printing in a different function.

    There's more but I'm guessing you get the drift...
    Last edited by CommonTater; 01-17-2012 at 04:08 AM.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Location
    Boise Idaho
    Posts
    4
    I got rid of my buffer problem, thanks. The reason the engVal function is still like that is because
    I was trying to have it return to main, however I am having difficultys figuring out a way to do so. (work in progress)
    I can return the engValue no problem, but I want the value with the notation with it..... That is where I am at with that.

    Also about the comments on my code thanks for the direction there as well. I knew that I was gonna get eaten up
    a bit posting my code in here, but I am getting the outcome I wanted.

    I also wanted to know what is wrong with using the clear screen function?
    Is there something else I can do to get somewhat of the same effect?

    thanks
    Last edited by Mick R. Groff; 01-17-2012 at 05:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resistor Color Decoder for a 6 band resistor
    By nivoca in forum C Programming
    Replies: 2
    Last Post: 06-25-2011, 12:44 PM
  2. Programming newbie seeking help!
    By jaja009 in forum C Programming
    Replies: 2
    Last Post: 02-19-2010, 12:12 PM
  3. Novice Programmer Humbly Requests Job Seeking Advice
    By steals10304 in forum General Discussions
    Replies: 7
    Last Post: 10-20-2009, 12:12 PM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  5. Seeking Help on my 1st C++ class program!
    By YevGenius in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2003, 03:14 PM