Jump to content

help a newbie in coding in C++

migeo

So I just started programming. I wanted to do a small program that will convert a decimal number to a binary. Unfortunately, I seem to have hit a pickle and I can't seem to resolve the issue. Here is my code:

 

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

using namespace std;

int main ()
{
	int a;
	int b;
	unsigned int n=0;
	int c=0;
	float d=0;
	
	cout << "Enter number to be converted to binary: ";
	cin >> a;
	d=(log2(a));
	//cin >> b;
	
	//while (a-(2^n))>=0;
	//{
	//n = n+1;
	//}
	
	if ((d%1.0) == 0)
	{
			c = pow(10,d);
	}
	else
	{	
		while ((a/2) > 0)
		{
		
			if ((a%2) == 0)
			{	
				//cout << a;
				a = a/2;
				c = c + 0 * pow(10,n);
				n = n + 1;
			}
			else
			{
				a = a/2;
				c = c +1 * pow(10,n);
				n = n + 1;
			}
		}
	}
	
	cout << c;

	return 0;
}

When I compiled it, I had this error message and I don't know what to do. Thank you very much!

 

Untitled.png

Link to comment
Share on other sites

Link to post
Share on other sites

The remainder operator only works on integer types, for similar functionality for floats/doubles you need to use fmod.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, fizzlesticks said:

The remainder operator only works on integer types, for similar functionality for floats/doubles you need to use fmod.

Thank you very much dear sir. it's too late for me to code here so I'll try it tomorrow.

Link to comment
Share on other sites

Link to post
Share on other sites

Computers already store numbers in binary, it's not like we use decimal computers these days, and you can use bit manipulation directly to get the results you're looking for.

Read about binary operators in C/C++ here : https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

Anyway, the program could look something like this :

int d,i,b[32];
int main()
{
  cin>>d; 
  while(d){
	b[i++]=d&1;
	d>>=1;
  }
  for(i--;i>=0;--i)
  cout<<b[i];
}

Let's consider a variable x.

If you apply the binary operator AND, &, with a 1, more exactly if you do x&1, this will give you the last bit in the binary representation of x.

The binary right shift operator gets rid of the least significant bit in the number, and shifts the entire binary representation to the right by however bits you specify it to. Consider x =0b1010=10. x>>1=0b101=5. x>>2=0b10=2. x>>3=0b1=1. x>>4=0b0=0. I think you should get it by now. Also, essentially, x>>y=x/2y.

With these in mind, you should understand what's going on in the code above.

 

Note: the end result in the array b is the binary representation of d, but in reverse order. That's why I printed its values in reverse.

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Nineshadow said:

--snip--

You could also use bitset.

int main() {
    int i;
    cin >> i;
    std::bitset<sizeof(int) * 8> bs(i);
    std::cout << bs << '\n';
    return 0;
}

However while the alternatives are nice, converting an int to binary without these "cheats" is one of the first assignments in every programming class I've ever seen so doing it the long way is still good practice.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

On 9/24/2016 at 3:48 AM, Nineshadow said:

Computers already store numbers in binary, it's not like we use decimal computers these days, and you can use bit manipulation directly to get the results you're looking for.

Read about binary operators in C/C++ here : https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

Anyway, the program could look something like this :


int d,i,b[32];
int main()
{
  cin>>d; 
  while(d){
	b[i++]=d&1;
	d>>=1;
  }
  for(i--;i>=0;--i)
  cout<<b[i];
}

Let's consider a variable x.

If you apply the binary operator AND, &, with a 1, more exactly if you do x&1, this will give you the last bit in the binary representation of x.

The binary right shift operator gets rid of the least significant bit in the number, and shifts the entire binary representation to the right by however bits you specify it to. Consider x =0b1010=10. x>>1=0b101=5. x>>2=0b10=2. x>>3=0b1=1. x>>4=0b0=0. I think you should get it by now. Also, essentially, x>>y=x/2y.

With these in mind, you should understand what's going on in the code above.

 

Note: the end result in the array b is the binary representation of d, but in reverse order. That's why I printed its values in reverse.

 

 
 
 
 
 

 

21 hours ago, fizzlesticks said:

You could also use bitset.


int main() {
    int i;
    cin >> i;
    std::bitset<sizeof(int) * 8> bs(i);
    std::cout << bs << '\n';
    return 0;
}

However while the alternatives are nice, converting an int to binary without these "cheats" is one of the first assignments in every programming class I've ever seen so doing it the long way is still good practice.

 
 
 
 
 

Thank you! both of you. I understand the code above nineshadow and fizzlesticks thanks for telling me about fmod. now I noticed that my program still has a few bugs since while 8 gives me the right answer of 1000, when I inputed 3, it printed 1. I'm going to have to debug it now to see what's the problem. By the way, the day that I started this thread, our prof taught us about bitwise operator. Our task is now to create a program that will tell us what is the bit value at a certain place for example the bit value f 8 at the third spot is 0. I already kinda have an idea on how to do this, it's with >> and &. Either way, thank you!

 

Link to comment
Share on other sites

Link to post
Share on other sites

On September 24, 2559 BE at 2:06 PM, migeo said:

Thank you very much dear sir. it's too late for me to code here so I'll try it tomorrow.

One thing you could try is actually using logical shifts to get the individual bits and fill a string with the values of the bits.

 

Something like

 

int input;

string out (32);

for(uint i=0; i<32;++i)

{

    input << 1;

    //retrieve bit, cast to int, string[ i ] = bit

}

 

As for making it a generic function for floats and integers, you seem to be on your way.

 

edit: well, someone beat me to it AND got the bit retrieval operator. Oh well.

Software Engineer for Suncorp (Australia), Computer Tech Enthusiast, Miami University Graduate, Nerd

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×