Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m writing a program in C++ that converts numbers of different bases. Here are

ID: 3625125 • Letter: I

Question

I'm writing a program in C++ that converts numbers of different bases. Here are the sub functions. The first two are working properly but I am stuck on the one the converts Base10 numbers to other bases (fromBase10).

int digitToValue (char digit)
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', $
for (int i=0; i<36; i++)
{
if (digits[i]==digit)
return i;
}
}

char valueToDigit (int value)
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', $
for (int i=0; i<36; i++)
{
if (i=value)
return digits[i];
}
}

string fromBase10 (long base10Num, long base)
{
long length;
length=base10Num/((base*base)-base-1);
char nonBase10Num[length];
for (int i=0; i<length; i++)
nonBase10Num[i]=valueToDigit(base10Num);
return nonBase10Num;

long toBase10 (string nonBase10Num, long base)
{
long base10Num=0;
int length=nonBase10Num.length();
int values[length];
for (int i=0; i<length; i++)
{
values[i]=digitToValue(nonBase10Num[i]);
base10Num=base10Num+(values[i]*pow(base, length-i-1));
}
return base10Num;
}

Explanation / Answer

please rate - thanks

only converts to or from base 10

#include <iostream>
using namespace std;
long toBase10 (string, long );
int digitToValue (char);
char valueToDigit (int );
string fromBase10 (long, long );
int main()
{string number;
long from,base10,base;

cout<<"What base is the number to be converted? ";
cin>>from;
cout<<"What number would you like to convert? ";
if(from==10)
    cin>>base10;   
else
    cin>>number;   
cout<<"What base would you like to convert to? ";
cin>>base;
if(from==10)
cout<<base10;
else
   cout<<number;
cout<<" base "<<from<<" in base "<<base<<" is: ";
if(from==10)
    cout<<fromBase10(base10,base)<<endl;
else
    cout<<toBase10(number,from)<<endl;
system("pause");
return 0;
}

long toBase10(string nonBase10Num, long base)
{long base10Num=0;
int k=1,n;
int length=nonBase10Num.length();
for (int i=length-1; i>=0; i--)
{
n=digitToValue(nonBase10Num[i]);
base10Num+=n*k;
k*=base;
}
return base10Num;
}
int digitToValue (char digit)
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X'};
for (int i=0; i<36; i++)
{
if (digits[i]==digit)
return i;
}
}

char valueToDigit (int value)
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X'};
for (int i=0; i<36; i++)
{
if (i==value)
return digits[i];
}
}

string fromBase10 (long base10Num, long base)
{
long length=0;
int k=1;
do
{k*=base;
length++;
}while(k<base10Num);
char nonBase10Num[length];
for (int i=0; i<length; i++)
    {k=base10Num%base;
    base10Num/=base;
     nonBase10Num[length-i-1]=valueToDigit(k);
    }
return nonBase10Num;
}