Skip to main content
Code Review

Return to Revisions

5 of 5
edited tags
200_success
  • 145.5k
  • 22
  • 190
  • 478

Binary to decimal converter

I started a few weeks ago to learn C++ online. I just made a little binary → decimal program. Here is my code:

#include <iostream>
using namespace std;
int main()
{
 int i,j=1;
 long long decimal=0, powerOfTwo=1;
 cin>>i;
 char binary[i];
 i*=2;
 while (i)
 {
 if (i > j)
 {
 cin>>binary[j];
 ++j;
 }
 else
 {
 cout<<binary[i];
 if (i%4 == 1) //adds a space after 4 numbers (eg. 1011 0111 instead of 10110111)
 cout<<" ";
 if (binary[i] == '1')
 decimal += powerOfTwo;
 powerOfTwo *= 2;
 }
 --i;
 }
 cout<<"in ten base is equal to "<<decimal;
 return 0;
}

So I want to know, am I heading in the right direction? Please let me know what I am not doing good or any kind of suggestion.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /