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.
#include <iostream>
usingnamespace 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.
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.