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.