Problem with a binary to decimal conversion program. Help please.
So I tried debugging this program almost 10 times, each time finding a problem then making code to fix it, I used to get wrong values but then now after I changed the calculation from an infinity while loop to a for loop (because after some changes I realized that now I know the number of entries, meaning the start and the end of the loop) the program now displays no values at all. I checked it thoroughly and until the calculation process its functioning exactly the way it should but then the calculation breaks hell loose :frown:. Also the for loop that raises 2 to the power needed works fine too so it must be something with the other processes included in the calculation. I would be grateful for any help I receive. Thank you.
PS:This used to be a function of a multiple value types conversion program, I isolated it for easier analysis as a lone standing program.
Code:
#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 is 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(100th or 10th etc)
anse*=2;
bind+=e[i]*anse; //Multiplies it by the digits as required
}
cout<<bind; //Decimal value answer
return 0;
}