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.
- 81
- 5
- 81
- 5