Hi,

I know that there are many topics about this, but, i read a lot of them, and no one solved my problem.

I doing some exercises of the book "Deitel How to Program, 5", and in the exercise 4.27 i need to write a program that get a binary integer from user, then print it in decimal, but i can only use the programming techniques that i have learned in the book, here is a shell script that i have written to do it, if i write this in c++ i will be done.

#!/bin/sh

binary=$1
numberOfChars="`echo $binary |wc -c`"
counter="1"
decimal="0"

while [ $counter -lt $numberOfChars ]
do

   if [ `echo $binary |cut -c $counter` -lt 0 ] || [ `echo $binary |cut -c $counter` -gt 1 ]
      then

         echo "A Binary number contains only 0s e 1s."

         exit 0

   fi

   decimal=`expr $decimal \* 2 + \`echo $binary |cut -c $counter\` `

   counter=`expr $counter + 1`

done

echo "Decimal: $decimal"

exit 0

The problem is that in C++ i don't know how can i use something like "cut", there is a hint in the book, saying that i can use module and division operators to get the digits, but i only know how to do it with a predetermined number of digits.

Obs: Please, i cannot use "for", and i can only use the iostream and string libs.

Dani AI

Generated

A quick, book‑friendly approach that answers and builds on 's hint: read the user input as a std::string and scan it with a while loop. For each character validate it is 0 or 1, then update an accumulator with acc = acc * 2 + (bit). This mirrors the shell script's per‑character cut but uses s[i] in C++. It preserves leading zeros and avoids treating the input as a decimal integer.

#include <iostream>
#include <string>

int main() {
    std::string bin;
    if (!(std::cin >> bin)) return 0;

    unsigned long long decimal = 0;
    std::size_t i = 0;
    while (i < bin.length()) {
        char c = bin[i];
        if (c != '0' && c != '1') {
            std::cout << "A Binary number contains only 0s and 1s.\n";
            return 0;
        }
        decimal = decimal * 2 + (c - '0');
        ++i;
    }

    std::cout << "Decimal: " << decimal << '\n';
    return 0;
}

Notes and gotchas:

  • This uses only <iostream> and <string> and no for loops, matching your constraints.
  • If the binary string is longer than about 64 bits, unsigned long long will overflow. For arbitrarily large input use a big‑integer library (e.g., Boost.Multiprecision) or implement string-based addition/multiplication.
  • If you prefer the modulus/division technique from the book, it works when you treat the input as a numeric value and extract digits; be careful because reading into an integer will interpret the literal as decimal unless you process its digits manually.
  • To accept optional prefixes like 0b or a leading sign, strip those characters first before the main loop.

Thanks to for the modulus suggestion and for clarifying the goal; the string scan above keeps the solution simple and robust for the exercise.

Recommended Answers

All 3 Replies

>>i don't know how can i use something like "cut",
There is no such term.

It's pretty easy to get the digits

int x = 123;
int digit;
while(x > 0)
{
   digit = x % 10;
   x /= 10;
}

I'm a little confuse...Do you want a program that will take an inputted binary number and then produce its decimal equivalent?

e.g.

enter a binary number->111
ans->7

Is that what you want?

I'm a little confuse...Do you want a program that will take an inputted binary number and then produce its decimal equivalent?
Yes

It's pretty easy to get the digits
Thanks, this is what i was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.