I am writing a function that will convert from one base to the next. I the progr
ID: 3853005 • Letter: I
Question
I am writing a function that will convert from one base to the next. I the program working, but I want to be able to input GAG 22 31 where GAG is the number in the given base ie A = 10 B = 11 C=12 ect
and get GAG 22 89D 31. Can someone help me fix this please?
#include
#include
#include
#include
using namespace std;
int toBaseTen(int oldBase, string oldNum){ // Function converts oldnum and oldBase to base 10
int tenNum = 0;
int i = oldNum.length();
int j = 0;
int temp = 0;
while (i){ //if user enters letters, convert the letters into their numerical representation.
temp = 0;
if('0'<= oldNum[j] && oldNum[j]<='9'){
temp = oldNum[j]-48;
}
else if ('A' <= oldNum[j] && oldNum[j] <='Z'){
temp = oldNum[j] - 'A' + 10;
}
tenNum += temp*pow(oldBase,i-1); //converts the number into its base ten form
i--;
j++;
}
return tenNum;
}
int toNewBase(int BaseTenNum,int newBase){ //Function converts base ten number to a user-defined base.
int x = BaseTenNum;
int y = 0;
int i = 0;
while(x/newBase){ //divide by new base storing the result in x until quotient reaches zero.
y = y+((x-(x/newBase)*newBase)*pow(10,i)); // y is the sum of each number in the ith position.
x/=newBase;
i++; //i is the position of the string of numbers
}
y = y+(x-(x/newBase)*newBase)*pow(10,i);
return y;
}
int main()
{
int oldBase = 0;
string oldNum;
int newBase = 0;
int BaseTenNum = 0;
int ConvertedNum = 0;
cout<<"Please enter the number's base: ";
cin>>oldBase;
cout<<"Please enter the number: ";
cin>>oldNum;
cout<<"Please enter the new base:";
cin>>newBase;
BaseTenNum = toBaseTen(oldBase,oldNum); // Function converts original base and number to base 10 and returns the number
ConvertedNum = toNewBase(BaseTenNum, newBase); // Function converts the base 10 number to desired new base.
Explanation / Answer
Here is the completed code for your question. Post a comment in case of any issues. Please don't forget to rate the answer if it helped. Thank you very much.
#include <cmath>
#include <iostream>
using namespace std;
long toBaseTen(int oldBase, string oldNum){ // Function converts oldnum and oldBase to base 10
long tenNum = 0;
int idx = oldNum.length() - 1;
int power = 0;
int num = 0;
while (idx >= 0){
//if user enters letters, convert the letters into their numerical representation.
num = 0;
if('0'<= oldNum[idx] && oldNum[idx]<='9'){
num = oldNum[idx]-48;
}
else if ('A' <= oldNum[idx] && oldNum[idx] <='Z'){
num = oldNum[idx] - 'A' + 10;
}
tenNum += num*pow(oldBase,power); //converts the number into its base ten form
idx --;
power++;
}
return tenNum;
}
string toNewBase(long BaseTenNum,int newBase){ //Function converts base ten number to a user-defined base.
long x = BaseTenNum;
string newBaseNum;
int remainder;
while(x > 0){
//divide by new base storing the result in x until quotient reaches zero.
remainder = x % newBase;
if(remainder < 10) //0- 9
newBaseNum = (char)( '0' + remainder) + newBaseNum;
else //number is 10, 11, 12 etc
{
newBaseNum = (char)( 'A' + remainder - 10) + newBaseNum;
}
x /= newBase;
}
if(newBaseNum.empty())
newBaseNum = "0";
return newBaseNum;
}
int main()
{
int oldBase, newBase = 0;
long BaseTenNum = 0;
string oldNum, ConvertedNum;
cout<<"Please enter the number's base: ";
cin>>oldBase;
cout<<"Please enter the number: ";
cin>>oldNum;
cout<<"Please enter the new base:";
cin>>newBase;
BaseTenNum = toBaseTen(oldBase,oldNum); // Function converts original base and number to base 10 and returns the number
ConvertedNum = toNewBase(BaseTenNum, newBase); // Function converts the base 10 number to desired new base.
cout << oldNum << " in base " << oldBase << " is " << ConvertedNum << " in base " << newBase << endl;
}
output
Please enter the number's base: 22
Please enter the number: GAG
Please enter the new base:31
GAG in base 22 is 89D in base 31
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.