Thread: Strings & Recursive functions

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    4

    Strings & Recursive functions

    I've spent a little over a week trying to get a handle on strings. I've gone to my teacher (the help she was able to give me she did by looking at the answer key because the wording and instructions in the assignment confused her as well.

    I've got a pretty good grasp on C programming as a whole, just took my final and got a 100 on it, but writing strings is an area that I'm struggling with a lot. I've gone to youtube, google, the book, you name it. This is the only thing keeping me from getting the dean's list and I wouldn't seek help in this manner if I hadn't exhausted all other methods.

    My code is supposed to take user inputs for the colors of a resistor, make them user string all lower case, replace grey with gray and purple with violet, and then finally display the numerical equivalent of the color band. Below is what I have this far. I have an idea of what I need to do, but everything I've tried thus far hasn't produced the desired results

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    /*color codes*/
    char COLOR_CODES[10][7] = {"black", "brown","red","orange","yellow","green","blue","violet","gray","white"};
    
    
        int readColor(int BandNumber) {
        int colorNumber = -1;
        int i;
    
    
        /* Decalre a string to hold the color name typed in at the keyboard. */
    
    
        do { 
            
        /* Prompt to read color name for the indicated band.*/
    
    
                     printf(" "    );
        /*read a color band name as a string.*/
    
    
        _strlwr();
        for (i = 0; (i < 10) && (colorNumber == -1); ++i) {
           if (strcmp(colorBandName, COLOR_CODES[i]) ==0) {
                    
                      colorNumber = i;
    }
    }
         if (colorNumber == -1);
                  printf("The color"%s"was not recognized.\n");
                  printf("Please re-enter.\n", colorBandName);
    }   while (colorNumber == -1);
    return (colorNumber);
    }
    
    
    void outresistance(double Resistance) {
        printf("resistance is:");
        
        If (Resistance <= 999.95){
        } else if()
     }else if
    }else
    Printf("%.1fMEga=ohms.\n",resistance/100000.00);
    }
    }
    int Main(void){
    int band1,band2,band3;
    double resitance;
    char yesNoAnwers[20];
        char colorBandName[20];
    do{
    printf("enter the three color bands of a resistor,");
    printf("one at a time as prompted.\n");
    band1 = readColor(1);
    band2 = readColor(2);
    band3 = readColor (3);
    outputResistance(resistance);
    printf("[answers Y or N]");
    scanf("s%",colorBandName);
    _strlwr( );
    }while (/*compare answer for yes*/);
    return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I've got a pretty good grasp on C programming as a whole, just took my final and got a 100 on it
    If it was an exam for 10 year olds perhaps.
    But bold assertions like "pretty good grasp" when you post code full of so many basic mistakes doesn't bode well.

    Before posting, make sure it at least compiles.
    If it doesn't compile, because you don't understand some error message, then still post the code and your error messages.

    Randomly put together bits of code and pseudo-code will only generally attract, "yeah, that looks about right" (or not, as the case may be).


    > the help she was able to give me she did by looking at the answer key because the wording and instructions in the assignment confused her as well.
    The blind leading the blind.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to format your code properly, especially concerning indentation. Furthermore, you need to write code that will actually compile, or least make a best effort in that direction. This means taking care about the case-sensitivity of names (e.g., if versus If, printf versus Printf, main versus Main).

    I suggest that you put aside your current code for now and implement the basics of what you need to do, e.g.,
    Code:
    void normalizeColorName(char colorName[]);
    void printNumericalColorBand(const char colorName[]);
    
    int main(void)
    {
        char colorName[] = "Grey";
        normalizeColorName(colorName);
        printNumericalColorBand(colorName);
        return 0;
    }
    So, normalizeColorName will make the color name lowercase and change "grey" to "gray", then printNumericalColorBand will print the numerical color band corresponding to gray. From this basic code, you can add and test with more colors, until you have covered all the valid colors. After that, you can go on to deal with user input and invalid colors. Finally, you can wrap it in a loop to deal with all three color bands for the resistor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    Quote Originally Posted by Salem View Post
    > I've got a pretty good grasp on C programming as a whole, just took my final and got a 100 on it
    If it was an exam for 10 year olds perhaps.
    But bold assertions like "pretty good grasp" when you post code full of so many basic mistakes doesn't bode well.

    Before posting, make sure it at least compiles.
    If it doesn't compile, because you don't understand some error message, then still post the code and your error messages.

    Randomly put together bits of code and pseudo-code will only generally attract, "yeah, that looks about right" (or not, as the case may be).


    > the help she was able to give me she did by looking at the answer key because the wording and instructions in the assignment confused her as well.
    The blind leading the blind.
    I agree, while "a good grasp" is a relative statement. I'm just getting started in programming. My major is electrical engineering, but I want to have a general idea in the subject.

    I feel my main problems at this time is I don't know the proper syntax required to get my program to read the user inputs and assign them to a string, and the _strlwr function, I've read up a bit on it, but I don't have the formula correct for it to make the arguments.

    Thank you for taking the time to read this and provide feedback. It's greatly appreciated. Also, Happy Thanksgiving

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    Thanks Laser, I was still stuck on having an insufficient number of arguments for _strlwr. I always work my way down the error messages when I debug, though using the capital letters is a rookie mistake lol. Thank you for your feed back and taking the time to read this. I'll take a look at your suggested code input and see if I can get a good understanding of it. Happy Thanksgiving

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shaviun Jones
    I was still stuck on having an insufficient number of arguments for _strlwr.
    You should keep in mind that _strlwr is non-standard, though it is likely to be available in the standard library implementation that comes with Microsoft compilers. Speaking of those, the MSDN entry for _strlwr reads:
    Code:
    char *_strlwr(
       char * str
    );
    Contrast the above declaration with what you wrote in your attempt to call it:
    Code:
    _strlwr( );
    Anyway, although it may be more convenient, you do not need _strlwr. You could use the standard tolower function declared in <ctype.h>, though you would then need a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    Laser you might be my favorite person this year. Thanks for all of your help and I hope you are showered with blessings and happiness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive functions
    By George Ioannou in forum C Programming
    Replies: 6
    Last Post: 11-25-2015, 11:11 AM
  2. recursive function with strings
    By alzar in forum C Programming
    Replies: 5
    Last Post: 01-19-2008, 11:38 AM
  3. Recursive Functions
    By jack999 in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2006, 01:28 PM
  4. Recursive Functions
    By zz3 in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2004, 08:40 AM
  5. recursive functions
    By doug in forum C Programming
    Replies: 1
    Last Post: 02-24-2002, 10:54 AM

Tags for this Thread