1

So im working on a class assignment where I need to take a base 2 binary number and convert it to its base 10 equivalent. I wanted to store the binary as a string, then scan the string and skip the 0s, and at 1s add 2^i. Im not able to compare the string at index i to '0, and im not sure why if(binaryNumber.at(i) == '0') isnt working. It results in an "out of range memory error". Can someone help me understand why this doesnt work?

#include <iostream>
using namespace std;

void main() {
    string binaryNumber;
    int adder;
    int total = 0;

    cout << "Enter a binary number to convert to decimal \n";
    cin >> binaryNumber;
    reverse(binaryNumber.begin(),binaryNumber.end());

    for (int i = 1; i <= binaryNumber.length(); i++) {
        if(binaryNumber.at(i) == '0') { //THIS IS THE PROBLEM
        //do nothing and skip to next number
        }
        else {
            adder = pow(2, i);
            total = adder + total;
        }
    }

    cout << "The binary number " << binaryNumber << " is " << total << " in decimal form.\n";
    system("pause");
}
  • 1
    In C++, [use int main() instead of void main()](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Nevus Sep 18 '21 at 02:06
  • What is the reason? I usually do, but my teacher supplied the skeleton main () function and it was void. – PrometheusAurelius Sep 18 '21 at 02:53
  • Its not valid C++ so some compilers may give error. [See example](https://godbolt.org/z/79er3x9hr). – Nevus Sep 18 '21 at 03:42

3 Answers3

3

Array indices for C++ and many other languages use zero based index. That means for array of size 5, index ranges from 0 to 4. In your code your are iterating from 1 to array_length. Use: for (int i = 0; i < binaryNumber.length(); i++)

Nevus
  • 1,307
  • 1
  • 9
  • 21
  • Thank you!! I was staring at this for so long. I intentionally started at i = 1 to skip the first value since it will always be 0, and not needed for the calculation. For some reason I put <=, brain fart I guess. Anyway, thanks again! – PrometheusAurelius Sep 18 '21 at 01:38
  • @PrometheusAurelius The first value will not be 0. Take 1011(13) for example. If you reverse it it will be 1101. – Nevus Sep 18 '21 at 01:45
  • Another brain fart, lol. Thanks for pointing that out. My last program did the opposite of this one and always ended in a 0 (then after flipping it the 1st digit was zero) and I had to correct for it. – PrometheusAurelius Sep 18 '21 at 02:51
1

The problem is not with the if statement but with your loop condition and index.

You have your index begin at one, while the first character of a string will be at index zero. Your out memory range error is caused by the fact that the loop stops when less than or equal, causing the index to increase one too many and leave the memory range of the string.

Simply changing the loop from

for (int i = 1; i <= binaryNumber.length(); i++) {
    if(binaryNumber.at(i) == '0') {
    }
    else {
        adder = pow(2, i);
        total = adder + total;
    }
}

To

for (int i = 0; i < binaryNumber.length(); i++) {
    if(binaryNumber.at(i) == '0') { 
    }
    else {
        adder = pow(2, i);
        total = adder + total;
    }
}

Will solve the issue.

Stripes
  • 11
  • 1
0

Because your started from 1 and not 0

for (int i = 1; i <= binaryNumber.length(); i++)

Try with that

for (int i = 0; i < binaryNumber.length(); i++)
Degik
  • 19
  • 5