0

I am writing a program that uses a recursive solution to convert a number to an indicated base. My issue is that the output to the screen only posts a single number (I'm guessing this is the last number). How can I properly return the remainders in order to the screen?

When viewing the call stack everything looks correct to me. I figure that I am missing something very simple or obvious.

Here is the section of my program that calls the BaseConverter funtion.

while (!File.eof())
{
    File >> Decimal;
    File >> Base;
    // Check if values are valid
    // function call BaseConverter (Decimal, Base);
    cout << BaseConverter(Decimal, Base) << endl;

}

Recursive function to convert a decimal number to an indicated base.

int BaseConverter(int Decimal, int Base)
{
// Anchor Point
   if (Decimal < Base)
    {
         return Decimal;
    }
// Recursively divides the decimal by the base. Returns the remainder of the       Decimal by the base while unwinding.
   else
    {
    return BaseConverter(Decimal / Base, Base) % Base;
    }
}
  • 2
    [`!File.eof()` is not a good loop condition](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – MikeCAT Sep 15 '16 at 03:10
  • To return reminders, return reminders. `std::vector` may be useful. – MikeCAT Sep 15 '16 at 03:11
  • You only have one output statement for each set of inputs... why do you expect more than 1 output? – M.M Sep 15 '16 at 03:13
  • If returning is not required, another way is to print results inside the recursive function. – MikeCAT Sep 15 '16 at 03:14
  • @MikeCAT How would I do that? When I try to have it "cout" where I have return it prints a bunch of (what I see as) garbage to the screen. – Darkstorm51 Sep 15 '16 at 03:16
  • Okay, what I mean to ask is... How do I have the function print to the screen as it is unwinding? That is what I am trying to achieve. – Darkstorm51 Sep 15 '16 at 03:20
  • Hmmm, your recursion contains bug. The most significant digit is returned and it is divided to calculate the reminder. – MikeCAT Sep 15 '16 at 03:23

2 Answers2

1

Because you are only returning single integer from function.

A correct implementation:

string ConvertBase(int num, int b) {

  if( b < 2 ||  b > 9 ) throw("Not supported");

  stringstream ss;

  if(num < b) {
      ss << num;
      return ss.str();
  }

  ss << (num % b);

  return ConvertBase(num/b, b) + ss.str();

}

Note that for base higher than 9, you will need to have characters to represent those numbers.

bits
  • 1,196
  • 13
  • 14
0

You don't need to work with strings, it's faster if you work with numbers (unless you need to worry about size constraints).

Coliru example:

#include <sstream>

using namespace std;

int BaseConverter(int Decimal, int Base)
{
// Anchor Point
   if (Decimal < Base)
        return Decimal;
   else
        return BaseConverter(Decimal / Base, Base) * 10 + Decimal % Base;
}

int main()
{
    std::string str("32 2\n28 5");
    std::istringstream File(str);
    int Decimal, Base;

    while (!File.eof())
    {
        File >> Decimal;
        File >> Base;
        cout << BaseConverter(Decimal, Base) << endl;
    } // Outputs 10000 and 103
}
Peregring-lk
  • 9,237
  • 6
  • 37
  • 86