Need some help with recursion

This is a discussion on Need some help with recursion within the C++ Programming forums, part of the General Programming Boards category; Code: void writeback(char n[]) { string a; if(strlen(n)>1) { writeback(substr(n,1)); } cout << n[0]; } This is a function my ...

  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
    21,717
    What is the error message? What is substr?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,304
    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, 09: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, 02:53 PM

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