splitting a number into separate digits

im writing a homework program, no im not asking for you to do it for me, but its converting a number of an arbitrary base into a decimal number. i know how to convert it and all that jazz, but what im having trouble with is splitting the number up and putting it into an array so that i can actually convert it. This is what i have so far.

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
#include <iostream>
using namespace std;

int numAr[8];

void convert(int number, int base){
 
    
}

int main(){

    
    int number;
    int base;
    int decimal;
    
    
    cout << "enter the number to convert: " << endl;
    cin >> number;
    //split number into digits
    cin >> base;
   // convert( int number, int base);
    cout << "Your number " << number << " of base " << base << " is now " <<  decimal << " of base 10." << endl;
    
    return 0;
}


havent written the convert function yet cause thats easy, but i would appreciate guidance on my problem. I saw something reading around about like number = number %10 or something like that, but plz help.

thanks!
Last edited on
y = x % b assigns last digit of x in base b to y.
y = x / b assigns all digits of x except the last one, again in base b, to y.

First put the last digit of x into numArr[0], then the last digit of x/b into numArr[1], etc. That will be one loop. Note that this would put digits in reverse. It doesn't really make any difference.
Last edited on
Topic archived. No new replies allowed.