A problem with a program that converts binary code to decimal code.Help if you may please.

This program basically asks you to input the digits of you're binary number then converts them into decimal values.The problem was narrowed down to the calculation stage.It used to output wrong values ,ranging from much bigger to just bigger numbers than the needed values but now the program just stops after entry,it doesn't crash or anything just after I finish entry it stays there with no output what so ever and doesn't end,this started happening after I replaced an infinity while loop with a for loop because after a lot of changes I found out that now I can know the start and end points of the required loop so why risking it with an infinity loop right?


Additional information (how exactly the program should work):
It stores the binary digits in an array then afterwards copies them into a different array but backwards so that the digits start from the lowest value (meaning least significant value) then it starts the calculation.





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
#include<iostream>
using namespace std;
int main()
{
 
 
    int d[10],e[10],anse=1,r,limit;
    short int counterd=0,i,j;
    float bind=0;
    cout<<"You can now enter the digits of your binary number. End the entry by entering -1"<<endl;
 
 
 
 
 
 
    while(1)                                 //Entry stage 
    {
 
 
    cin>>d[counterd];
    if(d[counterd]==-1)
    {
        counterd--;                         //This is because the counter used to count even the entry of -1
        break;
    }
 
 
    counterd++;
    }
    r=counterd;                     //The counter value is copied to "r" so it decreases while "counterd" is the end of the for loop
    limit=r;                
     
    for(i=0;i<=counterd;i++)       //The digits are flipped so they start from the lowest instead of the highest in another array
    {
     
        e[i]=d[r];
        r--;
    }
 
 
                   
    for(i=0;i<=limit;i++)         //The calculation happens here (mistakes too aparently)
    {    
     
 
 
    for(j=i;j>=0;i--)             //This raises 2 to the needed power according to it's position value(from least significant value to most significant value)
    anse*=2;
     
     
    bind+=e[i]*anse;             //Multiplies it by the digits as required
    }
 
 
 
 
     
 
 
    cout<<bind;  //Decimal value answer
 
 
 
 
return 0;
}
 
Topic archived. No new replies allowed.