Write a complete, well-written, documented program (named convert) that prompts for and reads in a number with at most MAX_DIGITS (9) in one of the following four formats: binary, octal, decimal, and hexadecimal and converts it to a requested base.
Once the number has been read in, you should verify that the number is valid (more on this later). If it is not valid, you should print the appropriate error message described below and exit with the specified error code immediately. After you determine the input is valid, you should determine what format the number is in (based on the rules described below) and display the appropriate type. Afterwards, the program should prompt for the base that the number should be converted to. You should verify that the base is within the range MIN_BASE and MAX_BASE (inclusive). If not, you should print the error message and exit with the exit code described below. Finally, the program output the number in the entered base and asks you if you want to continue. If you enter the value – as an input the program exits. Otherwise, it repeats the above steps
Input Format:
The input format for each of the four required types is as follows:
 BINARY numbers will be prefixed with a capital B followed by 1-8 digits from 0-1.
 OCTAL numbers will begin with '0' (zero) and be followed by 1-8 digits from 0-7.
 DECIMAL numbers cannot begin with a '0' (zero) and must be followed by 1-9 digits from 0-9.
 HEXADECIMAL numbers must begin with “0x” or “0X” followed by 1-7 digits from 0-9 and A-F.
 The base will be input as an integer, as in '2' for binary, '8' for octal, '10' for decimal, '16' for hex
 You must support all bases from MIN_BASE (2) up through MAX_BASE (36).
 All input will be >= 0.
 Input is limited to a total of MAX_DIGITS (9) in length.
Output Format:
Your program must be able to convert any of the above types into a decimal using the to_dec function described below. Once converted, the number must be printable in any base from MIN_BASE – MAX_BASE (inclusive) using the function print_dec_base described below.

Recommended Answers

All 5 Replies

That's a nice problem.

When you show your work and ask specific questions, we'll be glad to help.

I cannot start with this code , I want just the idea how to make the functions

#include <iostream>

using namespace std;

int function()
{
    int a=0,b=0;
    a += b;

    cout << "This is a function\n" << a;

    return(0);
}

That is how to make functions... Now I guess you would start?

I know how to make a function , I want the idea how to convert to decimal in the function . Thank you :)

In general when faced with such problems you should try to do it yourself first. Typically grabbing pencil and paper is a good start. Write out a number in some base and try to convert it to some other base. First try a full example, then try to convert a generic example from one base to another, then try to do it where both bases are generic (IE: variables) and the number is generic. Once you can derive a nice formula or algorithm for base conversion you can implement a function like this:

char *convert(int initialBase, int finalBase, char *number)
{
    #warning todo: implement!
}

If you find that you cannot derive a formula for the general case there is a wonderful website called google that may be able to assist you. Once that fails you may ask here again, this time showing your previous attempts. This forum is not a homework hotline, it is an IT discussion forum.

Nonetheless I will give you some hints:

1) Since you are only dealing with relatively small numbers you can store their value in an integer type, thus you only have to find out how to convert any base to an actual integer value, and then how to output any integer value in any base. So your function can become:

int calculateValue(int base, char *number)
{
    #warning todo: implement!
}
char *valueToBase(int value, int base)
{
    #warning todo: implement!
}
char *convert(int initialBase, int finalBase, char *number)
{
    int val=calculateValue(initialBase,number);
    return valueToBase(val,finalBase);
}

2) Always keep in mind the definition of a base (IE: base X means that digits are X^0,X^1,etc)
3) Modularize your code! If you find an algorithm of the form:

First I will do this.
Then I will do that.

Then if this and that are very different tasks, you should probably write two functions to do them seperately. This makes your code much easier to read/debug/edit. Good Luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.