hex, binary, octal, and decimal conversions

im having a problem with this assignment, i already did the first part of the
problem which is
Take a byte, an 8 digit binary number as input, and output the corresponding
decimal number.
 Enter a decimal value, no larger than can be stored in a byte, and output the
corresponding binary and octal representations of the number.

 Enter an octal value, no larger than can be stored in a byte, and output the
corresponding decimal and binary representations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  int main() {
        //Here i declared the variables for the first function, to use for the binary number.
        int b_1;
        int b_2;
        int b_3;
        int b_4;
        int b_5;
        int b_6;
        int b_7;
        int b_8;
        int a,b,c,d,e,f,g,h,A,B,C,D,E,F,G,H,x,y; 
                                                 
        
        cout << "Welcome! " << endl;
        cout << "To convert from binary to decimal, please have the 8bit binary number ready, and follow the instructions. " << endl;
        cout << "Please enter the first digit of your binary number (from left to right). " << endl;
        cin >> b_8 ;
        cout << "Please enter the second digit of your binary number (from left to right). " << endl;
        cin >> b_7 ;
        cout << "Please enter the third digit of your binary number (from left to right). " << endl;
        cin >> b_6 ;
        cout << "Please enter the forth digit of your binary number (from left to right). " << endl;
        cin >> b_5 ;
        cout << "Please enter the fifth digit of your binary number (from left to right). " << endl;
        cin >> b_4 ;
        cout << "Please enter the sixth digit of your binary number (from left to right). " << endl;
        cin >> b_3 ;
        cout << "Please enter the seventh digit of your binary number (from left to right). " << endl;
        cin >> b_2 ;
        cout << "Please enter the eighth digit of your binary number (from left to right). " << endl;
        cin >> b_1 ;
        
        cout << "The decimal conversion for this binary number is: " << endl;
        cout <<( (b_1 * pow(2,0)) + (b_2 * pow(2,1)) + (b_3 * pow(2,2)) + (b_4 * pow(2,3)) + (b_5 * pow(2,4)) + (b_6 * pow(2,5)) + (b_7 * pow(2,6)) + (b_8 * pow(2,7)) ) << endl;
        
        cout << "To convert from decimal to octal, and binary, please have the decimal (8-bit) number ready, and follow the instructions. " << endl;
        cout << "Please enter your decimal number: " << endl;
        cin >> dec >> x;
        cout << "The number in octal is: " << oct << x << endl; //here it converts to octal number.
a = x % 2;
        x = x / 2;
        b = x % 2;
        x = x / 2;
        c = x % 2;
        x = x / 2;
        d = x % 2;
        x = x / 2;
        e = x % 2;
        x = x / 2;
        f = x % 2;
        x = x / 2;
        g = x % 2;
        x = x / 2;
        h = x % 2;
cout << "the number in binary is: " << h << g << f << e << d << c << b << a << endl;


this is just part of the problem, but im really confused on the second part

Write a C++ program from your design in Assignment #1.
Expand the problem from Assignment #1 to work with any number of digits in the
number, as well as hexadecimal numbers:
• Ask the user for the base of the number, e.g. binary, octal, decimal, or
hexadecimal.

• Ask the user for the number of digits in the number.

• Get the number from the user.

• Print an error message if the number has more digits than the user said it would
have.

• Take the number input by the user and output all the corresponding
representations of the number other than the representation given.


any hints on point 2,3, and 4 would be greatly appreciated
Why do you avoid using a looping structure? It will make your life easier and make your code more modular.
this was the first assignment so we still didnt take anything about loops when it was due :(
Topic archived. No new replies allowed.