Program that turns decimal base to any base from 2 to 16

So I`m trying to write a code for program that turns any Natural dec number to a number with any Natural base. Basically the task is to make such program for Natural numbers to convert to numbers with base from 2 to 16, but I am trying to generalize it and then exclude what is in task... I have been messing with the code for quite a while now and I hink I have completly messed it up. I will be thankfull for any pointers for what is wrong or what can I do better or what exclude...
I will be helpfull for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include<iostream>
using namespace std;

void convertdectob(int m, int n)            
{
     if (m == 0)
        return;
     int x = m % n;                         
     m /= n;                                
     if (x < 0)
        m += 1;                             
     convertdectob(m, n);                    
     cout<< x < 0 ? x + (n * -1) : x;        
        return;
}

int main()
{
    int m,n;
    cout<<"Enter the integer to convert(m): ";
    cin>>m;
    cout<<"Enter the base 2<=n<=16: ";
    cin>>n;
    if(n<2) cout<<"Not the right base"<<endl;
    if(n>16) cout<<"Not the right base"<<endl;
    if (m>0)
    {
        convertdectob(m, n);
    }
    else
        cout<<"0"<<endl;
    return 0;
}
You could use std::stoi to do the conversion for you.
http://www.cplusplus.com/reference/string/stoi/
I have never used this function, but I could look into it. Thank you!
Topic archived. No new replies allowed.