Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/922301445541715968
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
remove clutter from title
Source Link
Zeta
  • 19.6k
  • 2
  • 57
  • 90

Learning C++, am I heading in the right direction? (binary 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.

Learning C++, am I heading in the right direction? (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.

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.

My code transformed the binary number from right to left. Edited it to do it from left to right.
Link

Learning C++, am I heading in the right direction? (binary to decimal converter)

My code transformed the binary number from right to left. Edited it to do it from left to right.
Source Link
Loading
Source Link
Loading
lang-cpp

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