Please help with code

I have been trying to find the correct way to do this for a while now:
I need to convert either binary to all the bases but I keep running into errors.


This worked to convert binary to decimal, but when I change the power from 2 to another number it wont convert to the different base. Im not sure why this is.
1
2
3
4
    reverse(userNumber.begin(), userNumber.end());
    for (int i = 0; i < userNumber.size(); ++i) {
        decimal += (int(userNumber[i]) - 48)*pow(2, i);
    }


I have tried many different solutions to this problem but I cannot find the correct one.
I have also tried to convert the string to an array to try to make it easier on myself, but had no luck.

Does any one have any words of wisdom that could be passed my way.
All help will be appreciated


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 #include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int base=0;
    string userNumber;
    int decimal=0;
    cout<<"Enter the base of the number followed by the number "<<endl;
    cout<<"In the same line enter the bases you want it to be converted to"<<endl;
    cin>>base>>userNumber;
    
   //convert to dec
    reverse(userNumber.begin(), userNumber.end());
    for (int i = 0; i < userNumber.size(); ++i) {
        decimal += (int(userNumber[i]) - 48)*pow(2, i);
    }
    
    
    
    switch (base) {
        case 2:
            cout<<userNumber<<" (Base 2) "<<endl;
            break;
        case 3:

                cout<<decimal<<" (Base 3) "<<endl;
            
            break;
        case 4:

                cout<<decimal<<" (Base 4) "<<endl;
            
             break;
        case 5:

                cout<<decimal<<" (Base 5) "<<endl;
            
             break;
        case 6:

            cout<<decimal<<" (Base 6) "<<endl;
             break;
        case 7:

                cout<<decimal<<" (Base 7) "<<endl;
            
             break;
        case 8:
            cout<<oct<<decimal<<" (Base 10) "<<endl;
             break;
        case 9:

            cout<<decimal<<" (Base 9) "<<endl;
            
             break;
        case 10:
            
                cout<<dec<<decimal<<" (Base 10) "<<endl;
            
             break;
        case 11:

                cout<<decimal<<" (Base 11) "<<endl;
            
             break;
        case 12:

                cout<<decimal<<" (Base 12) "<<endl;
            
             break;
        case 13:

                cout<<decimal<<" (Base 13) "<<endl;
            
             break;
        case 14:
    
                cout<<decimal<<" (Base 14) "<<endl;
            
            break;
        case 15:

            
                cout<<decimal<<" (Base 15) "<<endl;
             break;
        case 16:

                cout<<hex<<decimal<<" (Base 16) "<<endl;
            
             break;
        default:
            cout<<"Base is not in range 2-16"<<endl;
            return 0;
            break;
    }
    
    
    cout<<"Base; "<<base<<" userNumber; "<<userNumber<<endl;
    
    return 0;
}

Change line 17:

decimal += (int(userNumber[i]) - 48)*pow(base, i); // Note: base

It will not work for anything greater than 10 because you need to deal with letters though

decimal += (int(userNumber[i]) - '0')*pow(base, i); // This might make it more obvious
I did change what you have noted and when I enter 101 in binary the base 3 conversion is 6 but should be 12. Im not sure why this is doing this.
101 with base 3 -> 10 dec

I get the correct answer. Strangely I don't get the right answer when I put 101 base 10. Then I get 100

It seems that
decimal += (int(userNumber[i]) - 48)*pow(base, i);
contains a mysterious rounding problem (even without the need of rounding).

To prevent this change the type of decimal to double
Topic archived. No new replies allowed.