I am studying in college and got stuck with an excercise in my book, should be simple but I insist of making life hard :p

Mission:
My code was sopoused to take a binary number with use of functions I have recently learned and mve all the digits to the right.
(the excercise actually said to take a number of 4 digits and didn;t mention wether to keep the 0 before the number or not)

Ex:
Like the number: 00101000 will become 00010100

problem:
The problem is I can't seem to find what;s wrong with the code but when I run it, it crushes (appearetnly it crushes after the first scan I tried to place a print code right after but it didn't print but crushed before) .
I didn't study yet what global parameters are, and I don;t know yet how to get the vector into the function.
And I dunno strings .

main idea:
I decided to store all numbers one digit at a time as a decimal in an integer.
I wanted to keep the the 0s before the first 1 and not make a number of 0010 into a 10...so I decided to count the 0s before the first digit of 1, and then my function will print the number of zeros +1 times the digit 0 (since I want to move all digits to the right , it adds another 0), then the function prints the number after the loop finishes printing the 0s.


my code:

#include <stdio.h>
void bin(int num,int count)
{
	for(int i=1;i<=(count+1);i++)
		printf("0");
	printf("%d",(num/10));

}

void main()
{	int num=0,count=0,a;  					
	
printf("Please enter the bin number, 1 digit at a time,at the end\nenter a number different than 0 or 1\n");
do 
		{
			scanf("%d",a);
			if ((a==0)&&(num==0)) count++;  
			 num=(num*10+a);
		}
			while ((a==0)||(a==1));
		if(num==1) printf("the number can not b divided by 2\n");
			if(num==0) printf("0/2=0\n");
			if((num!=0)&&(num!=1))
				bin(num,count);	
}

the parameters:
a= the digits each time a different one.
num= the 'bin like' decimal number.
counter= for counting the number of 0s before the number.
(I placed same names in the function for they represent same values).

the function is void since it;s for printinf number of 0s and then the number inside num.


I know I could turn it into decimal devide it by 2 and back into bin but I want the hard life :P....nah actually I already started this way so I insist of finding the bug :3

If you find anything please let me know~

>scanf("%d",a);
scanf expects a pointer to the object you want populated, so unless a is already a pointer (which it isn't), you need to pass the address to scanf:

scanf("%d", &a);

I suspect that's where your crash is coming from.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.