## C++: Binary to Decimal w/ Appearance of the Conversion Process

Im trying to make a program that converts binary to decimal but having the needs to show the process of the convertion

Enter a binary number: 10110

1*(2^4) + 1*(2^2) + 1*(2^1)

the decimal equivalent of 10110 is: 22

but instead the value of the counter in the middle of the loop fails to decrease, leading to this

SAMPLE IMAGE

this is my current code

```#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int main()
{
int bin, dec = 0, remainder, num, base = 1,counter=0,counter2=0, constnum;
cout << "Enter the binary number: ";
cin >> num;
bin = num;
constnum = num;
while(bin > 0)
{
bin=bin/10;
counter2++;
}

while (num > 0)
{
if (num % 10 == 1) {
cout << " 1*(2^" << counter2 << ") +";
counter2--;
}
else if(num % 10 == 0) {
counter2--;
}
remainder = num % 10; //get the last digit of the input
dec = dec + remainder * base;
base = base * 2;
num = num / 10;

}

cout << "\nThe decimal equivalent of " << constnum << " : " << dec << endl;
return 0;

}
```

Just use a bit set:

```#include <bitset>
#include <iostream>

int main()
{
std::bitset<32> val;
std::cin >> val;
std::cout << val.to_ulong() << "\n";
}
```

C Programming Tutorial for Beginners, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 "C" comes from the same letter as "G". The Semites named it gimel.The sign is possibly adapted from an Egyptian hieroglyph for a staff sling, which may have been the meaning of the name gimel.Another possibility is that it depicted a camel, the Semitic name for which was gamal.Barry B. Powell, a specialist in the history of writing, states "It is hard to imagine how gimel = "camel" can be

you should wirte the process in ascendant order, instead of descendant, this is to match your algorithim, which is in ascendant order

```if (num % 10 == 1) {
cout << " 1*(2^" << counter2 << ") +";
counter2--;
}
else if(num % 10 == 0) {
counter2--;
}
```

you should do this:

```if (num % 10 == 1) {
cout << " 1*(2^" << counter << ") +";
counter++;
}
else if(num % 10 == 0) {
counter++;
}
```

also, if you follow my recommendation, you may erase your first while:

```while(bin > 0)
{
bin=bin/10;
counter2++;
}
```

because is no longer needed

"C" Programming Language: Brian Kernighan, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 C-- (pronounced cee minus minus) is a C -like programming language. Its creators, functional programming researchers Simon Peyton Jones and Norman Ramsey, designed it to be generated mainly by compilers for very high-level languages rather than written by human programmers.

As already pointed out, you are calculating and displaying the result in different orders. I suggest that you store the result and display it later. Something like:

```#include <sstream>
....

int pos = 0;
string res;
while (num > 0) {
if (num % 10 == 1) {
stringstream out;
out << "1*(2^" << pos << ") + ";

res = out.str() + res;
}

remainder = num % 10; //get the last digit of the input
dec = dec + remainder * base;
base = base * 2;
num = num / 10;
pos++;
}

int len = res.length();
// to remove the last '+'
if (len >= 3) {
res = res.substr(0, len - 3);
}

cout << res;
```

Learn C, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.. When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.

C Tutorial, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

C Language - Overview, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system � Microsoft C++, C, and Assembler documentation. Learn how to use C++, C, and assembly language to develop applications, services, and tools for your platforms and devices.

Learn C Programming, C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs.C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This C++ tutorial adopts a simple and practical approach to describe the concepts of C++ for beginners to advanded software engineers.. Why to Learn C++. C++ is a MUST for students and working