How are you all group of this forum
I am from KSA , I am beginner in C++ language
Dear Members
I wrote C++ program to convert binary number to decimal numbers as show:
// this program to convert binary number to decimal number
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string bin;
int sum =0;
int x;
cout << "Give binary code: ";
cin >> bin;
x=bin.size()-1; // x is a variable to take last position in the string
cout<<"The previous binary number has "<<x+1<<" digit(s)"<<endl;
// the following loop to Inverse binary string
for(int i=bin.size()-1; i>=0; i--)
{
cout<<"bin["<<i<<"]:"<<bin[i];
cout<<endl;
// this step to convert
sum = sum + bin[i] * pow (2, (x-i));
}
cout <<"\nThe decimal number of "<< bin <<" is "<<sum;
cout<<endl;
return 0;
}
after compiler, when I write binary number as 001. the output was:
Give binary code:001
The previous binary number has 3 digit(s)
bin[2]:1
bin[1]:0
bin[0]:0
The decimal number of 001 is 337
press any key to continue
pleasssssssssss help me
regards.....
bin[i] holds a char, so '1' won't be 1 but something like 49