-1
char * recursivecombo(char *str, int choices, int level)
{
    int len = strlen(str);

    level++;
    if( level == choices)
    {   
            for (int i = 0; i < len -2; i++)

            {   

                   printf("%c", str[i]) ;
            }   
    }   
    else
    {   
        for (int i = 0; i < len - 2; i++)
        {   
                printf("%c",str[i]);
                recursivecombo(str.substr(1), level);

        }   
    }   
}

I want to use string instead of char*.

pmg
  • 106,608
  • 13
  • 126
  • 198
node ninja
  • 31,796
  • 59
  • 166
  • 254
  • 2
    How can you do this str.substr(1) on a char* ? And, you don't return anything from this function. In Java this would not be compileable, im not sure how C++ handles this, but I would assume some errors at least. – InsertNickHere Sep 25 '10 at 06:20
  • Why do you want to use string? Is it significantly better (faster, smaller, readable, portable) than this code? – JoshD Sep 25 '10 at 06:22
  • In addition to the issues raised above, your code won't compile because your recursive call doesn't have enough parameters. – Reinderien Sep 25 '10 at 06:28
  • If you fix your current code so it works and can compile it will be easier to help convert and change it from there. – Nope Sep 25 '10 at 07:04
  • If str was a string, then str.substr should return a string. – node ninja Sep 25 '10 at 07:33

2 Answers2

5
std::string recursivecombo(const std::string& str, int choices, int level)
{
    level++;
    for (int i = 0; i < str.length() -2; ++i)
    {
        cout<<str.at(i) ;
        if( level != choices)
            recursivecombo(str.substr(1),8,/*Missing choce*/ level);
    }  
/*Missing return value*/ 
}

This is just a mock-up using a string. Some issues with your function

1)Where is your return value

2)If you intend to use string use cout, rather than printf, if it is C++

3)Use prefix ++.

DumbCoder
  • 5,696
  • 3
  • 29
  • 40
2

As others have posted, you did not document a return, so this will be equivalent code:

string recursivecombo(const std::string & str, int choices, int level)
{
     what I wouldn't give for a holocaust cloak
}

I think what you probably meant was:

void recursivecombo(const std::string & strInput, int nChoices, int nLevel = 0);

implemented as:

void recursivecombo(const string & strInput, int nChoices, int nLevel /* = 0 */)
{
    nLevel++;
    if( nLevel == nChoices ) cout << strInput.substr(0,strInput.length()-2);
    else
    {
        for ( int i = 0; i < str.length() - 2; i++)
        {
            cout << str.at(i);
            recursivecombo(str.substr(1), nChoice, nLevel);
        }
    }
}
PatrickV
  • 2,057
  • 23
  • 30