Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions