简体   繁体   中英

Using a switch with a recursive function in C++

I am creating a program that coverts any base 10 number to any other base up to 36 using recursion and I cannot find the right spot to put my switch for changing any remainder that is returned by the recursive function larger than 9 into its letter counterpart. (A = 10, B = 11...) My conversion function uses recursion and seems to be making it a bit more difficult to implement the switch. Where would be the appropriate place to put the switch?

Converting 200 from decimal to hexadecimal should be c8. My program prints it as 128. The 12 should be c which is what the switch is for.

I am using recursion since it is a pretty simple way to convert to a new base. As of now the program runs but still continues to output just numbers and is not switching. Thanks for any replies!

#include <iostream>
#include <string>
#include<cstdlib>

using namespace std;

int baseConversion(int num, int base);

int main()
{
    int numInput;
    int baseInput;

    cout << "Enter number for conversion: " << endl;
    cin>>numInput;
    cout<<"Enter the base you would like the number converted to: "<<endl;
    cin>>baseInput;

//int temp2 = baseConversion(numInput,baseInput);


cout << numInput <<" to the base "<<baseInput<<" is: ";
switch(baseConversion(numInput,baseInput))
{
    case 10:
    cout << "A";
    break;
    case 11:
    cout << "B";
    break;
    case 12:
    cout << "C";
    break;
    case 13:
    cout << "D";
    break;
    case 14:
    cout << "E";
    break;
    case 15:
    cout << "F";
    break;
    case 16:
    cout << "G";
    break;
    case 17:
    cout << "H";
    break;
    case 18:
    cout << "I";
    break;
    case 19:
    cout << "J";
    break;
    case 20:
    cout << "K";
    break;
    case 21:
    cout << "L";
    break;
    case 22:
    cout << "M";
    break;
    case 23:
    cout << "N";
    break;
    case 24:
    cout << "O";
    break;
    case 25:
    cout << "P";
    break;
    case 26:
    cout << "Q";
    break;
    case 27:
    cout << "R";
    break;
    case 28:
    cout << "S";
    break;
    case 29:
    cout << "T";
    break;
    case 30:
    cout << "U";
    break;
    case 31:
    cout << "V";
    break;
    case 32:
    cout << "W";
    break;
    case 33:
    cout << "X";
    break;
    case 34:
    cout << "Y";
    break;
    case 35:
    cout << "Z";
    break;
    default:
        cout <<baseConversion(numInput,baseInput);
    }
    cout<<endl;

return 0;
}

int baseConversion(int num, int base)
{
if(num == 0 || base == 10)
    return num;

return (num% base) + 10*baseConversion(num / base, base);

}

`

Here is a crude recursive base conversion function to help you understand the difference between that and what you did:

std::string baseConversion(int num, int base)
{
   std::string result;
   if (num>=base) result = baseConversion(num/base, base);
   num %= base;
   if (num<10) result += (char)('0' + num); else result += (char)('A'+num-10);
   return result;
}

That function starts with an empty string when num<base or with a (recursive computation of) all but the rightmost digit when num>=base .

Then it appends the last digit to that.

you don't really need a switch, as also pointed in the comments : this can be done like so

string f(int n, int base) {
    string res = "";
    if (n < base) 
        res += n < 10 ? n : n + 'A' - 10;
    else {
        string t = f(n / base, base); 
        res += t + (n % base) < 10 ? n % base : (n%base) + 'A' - 10; //t should be on LHS
    }
    return res;
}

also, take care that you should return appending individual chars into either a string or a char array and not int data type as you are doing.

as again pointed out in comments, don't need recursion here unless just for learning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM