Fractional binary number to decimal number Write and test your own double abc (char * b) function in the program, taking in the form of a string the fractional binary number of the form "1101.101" and returning an equal decimal number (here 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0 + 1 * 2 ^ -1 + 0 * 2 ^ -2 + 1 * 2 ^ -3 = 8 + 4 + 0 + 1 + 0.5 + 0 + 0.125 = 13.625). The function should return the special value -1 if an incorrect fractional binary number is entered (e.g. containing a different digit than 0, 1 or more than one period '.').

My solution:

#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;
double abc(char* b);
int main() {
cout &lt;&lt;  abc("1101.101");
return 0;
}

double abc(char* b){
int i = 0;
int a = 0;
int dot = 0;
double sum=0;
int c = 0;
do{
    if ((b[a]=='1')||(b[a]=='0')||(b[a]=='.')){
        a++;
        if(b[a]=='.'){
            dot++;
        }
        if(dot==2){
            return -1;
        }
    } else {
        return -1;
    }
} while(b[a]!='\0');

while(b[i]!='.'){
    i++;
}
while(i!=0) {
    if(b[c]=='1'){
        sum = sum + pow (2, i-1);
    }
    c++;
    i--;
}
while(b[i]!='.'){
    i++;
}
if(b[i]=='.'){
    int m = 0;
    while(b[i]!='\0'){
        if(b[i]=='1'){
            sum = sum + pow (2, m);
        }
        m = m - 1;
        i++;
    }
}
return sum;
}


Problem:
Everything is fine, it gives me 13.625, but there is a lot of code. Is there any faster way to solve this exercise?
Here are some thoughts about your code:


Don't use using namespace std. This is considered bad practice and you can find many reasons on the internet why this is the case (for example here). You can, for example write std::cout instead.
You should use variable-names that tell the person who reads the code the purpose of the variable.
To improve the legibility of your code, you really should use indentation.
To further improve legibility, you can leave spaces between operators.
I made your code a bit shorter by avoiding redundant code. You could even make it a lot shorter still, but I wanted to maintain your code structure.
Finally, you should have a look at edge cases: Empty strings, strings that only contain "." and strings that don't have positions after/before the decimal point (for example 101 = 5 or .101 = 0.625).


The result looks like this:

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;cstring&gt;

double abc(char* b);

int main() {
    std::cout &lt;&lt;  abc(".101") &lt;&lt; "\n";
    return 0;
}

double abc(char* b){

    if(strcmp("", b) == 0 || strcmp(".", b) == 0) {       //Empty String or only "."
        return -1;
    }

    if(b[0] == '.') {       //No positions before the decimal point
        int length = std::strlen(b);
        char* newB = new char[length + 1];
        newB[0] = '0';
        for(int k = 0; k &lt; length; k++) {
            newB[k + 1] = b[k];
        }

        b = newB;
    }

    int countDots = 0;
    int index = 0;
    int dotIndex = -1;
    do{
        if ((b[index] == '1') || (b[index] == '0') || (b[index] == '.')){
            index++;
            if(b[index] == '.'){
                countDots++;
                dotIndex = index;
            }
            if(countDots == 2){
                return -1;
            }
        } 
        else {
            return -1;
        }
    } while(b[index] != '\0');

    if(dotIndex == -1) {    //No dot found, but not empty string, so you have a natural number
        int length = std::strlen(b);
        char* newB = new char[length + 2];
        for(int k = 0; k &lt; length; k++) {
            newB[k] = b[k];
        }
        newB[length] = '.';
        newB[length + 1] = '0';
        dotIndex = index;
        b = newB;
    }

    double sum = 0;
    index = 0;
    int exponent = dotIndex;

    while(exponent != 0) {
        if(b[index] == '1'){
            sum = sum + pow (2, exponent-1);
        }
        index++;
        exponent--;
    }

    while(b[index] != '\0'){
        if(b[index] == '1'){
            sum = sum + pow (2, exponent);
        }
        exponent = exponent - 1;
        index++;
    }

    return sum;
}



I will leave it to you to comment the code properly.

Please note: The code now is a bit longer than yours, but it is able to handle more things.



EDIT:

After one of the comments below, I decided to also show a version without C-style arrays, and without new. This also should solve the problem with possible memory leaks. So all in all, this is the better version (I will leave the old version, because the questioner asked for a function double abc(char* b)):

#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;string&gt;

double abc(std::string b);

int main() {
    std::cout &lt;&lt;  abc("1101.101") &lt;&lt; "\n";
    return 0;
}

double abc(std::string b){

    if(b.compare("") == 0 || b.compare(".") == 0) {       //Empty String or only "."
        return -1;
    }

    if(b[0] == '.') {       //No positions before the decimal point
        b.insert(0, "0");
    }

    int countDots = 0;
    size_t index = 0;
    int dotIndex = -1;
    do{
        if ((b[index] == '1') || (b[index] == '0') || (b[index] == '.')){
            index++;
            if(b[index] == '.'){
                countDots++;
                dotIndex = index;
            }
            if(countDots == 2){
                return -1;
            }
        } 
        else {
            return -1;
        }
    } while(index &lt; b.length());

    if(dotIndex == -1) {    //No dot found, but not empty string, so you have a natural number
        b.push_back('.');
        b.push_back('0');
        dotIndex = index;
    }

    double sum = 0;
    index = 0;
    int exponent = dotIndex;

    while(exponent != 0) {
        if(b[index] == '1'){
            sum = sum + pow (2, exponent-1);
        }
        index++;
        exponent--;
    }

    while(index &lt; b.length()){
        if(b[index] == '1'){
            sum = sum + pow (2, exponent);
        }
        exponent = exponent - 1;
        index++;
    }

    return sum;
}