Thread: Need some help with recursion

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Need some help with recursion

    Code:
    void writeback(char n[])
    {
        string a;
        if(strlen(n)>1)
        {
            writeback(substr(n,1));
        }
        cout << n[0];
    }
    This is a function my professor wrote on the board. It is supposed to write a word in reverse order. However I cannot seem to compile and run it.

    I have copied down exactly as he has written it on the board but the part where I bold throws off an error during compile. Could you see what is wrong with this code?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What is the error message? What is substr?
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Not sure what substr is. I just copied it down as he had it on the board. I tried using #include <string> but it still gives the error. The error says substr is undefined.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yeah, so you probably need to check your notes for where your professor defined substr. Otherwise, what you can do is to replace writeback(substr(n,1)) with writeback(n + 1).

    That said, I note that the local string object named a is not used. Perhaps you simply copied down the example incorrectly.
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It probably assumes one has already created a substr function. In this instance the following, although not a fully fledged proper substring function, would be enough to make it work:
    Code:
    char *substr(char *str, int pos)
    {
        return &str[pos];
    }
    However his code still has a bug in it in that it tries to output the null char if given an empty string. The fix would be to move the last line inside the if-statement.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion
    By mirfanud in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2009, 10:10 PM
  2. Recursion
    By trevordunstan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 11:36 PM
  3. Recursion
    By freddyvorhees in forum C++ Programming
    Replies: 15
    Last Post: 09-10-2008, 08:57 AM
  4. can someone help me out with recursion
    By JOsephPataki in forum C Programming
    Replies: 10
    Last Post: 05-13-2003, 04:55 PM
  5. Recursion
    By scottmanc in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 03:53 PM