HI everyone, I hopw someone out there can help me as this problem is really annoying me. I'm trying to write a program that adds two 2's complement binary numbers together. I have done this using cases, and a switch statement. When compliling there are no errors, but when i go to execute it, it doesn't print out anything. I've checked to see where my code is failing by putting printf("I'm here!"); after the switch statement, but this does not print out, so it seems to be my switch statement that is wrong. I have not used switch or cases before so could someone please have a look at my program to see where i'm going wrong.
Here is my code:
#include <string.h>
#include <stdio.h>
#include <math.h>
void bintodec(int);
void printbin(void);
char store[32]={0};
char addbin1[32]={0};
char addbin2[32]={0};
char answer[32]={0};
int count=0;
int n=0;
int i=0;
int result=0;
int length,ab_length;
int leftbit=0;
int j=0;
int cb=0;
int k=0;
unsigned int Total=0;
int main()
{
printf("Enter binary number: ");
scanf("%s", &store);
printbin();
bintodec(Total);
printf("Please enter two equal length binary numbers\n");
printf("1st binary number: \n");
scanf("%s", &addbin1);
printf("2nd binary number: \n");
scanf("%s", &addbin2);
printf("These are your two chosen binary numbers:\n 1st: %s\n 2nd: %s\n", addbin1, addbin2);
ab_length=strlen(addbin1)-1;
for(j=ab_length;j>=0;j--)
{
if(addbin1[j] =='0')
{
switch(addbin2[j])
{
printf("I'm here!");
case '0':
{
if(cb =='0')
{
answer[j] = '0';
}
else if (cb == '1')
{
answer[j] ='0';
}
}
case '1':
{
if(cb=='0')
{
answer[j] ='0';
cb = '1';
}
}
}
}
else if(addbin1[j] =='1')
{
switch(addbin2[j])
{
case '0':
{
if(cb == '0')
{
answer[j] ='1';
}
else if(cb=='1')
{
answer[j]='0';
cb = '1';
}
}
case '1':
{
if(cb == '0')
{
answer[j] = '0';
cb = '1';
}
else if (cb == '1')
{
answer[j] = '1';
cb = '1';
}
}
}
}
}
if(cb == '1')
{
printf("overflow has occurred");
}
printf("Your answer is: %s ", answer);
for(k=ab_length; k>=0; k--)
{
printf("%c", answer[k]);
}
return 0;
}
void printbin(void)
{
printf("The binary number you entered is: %s \n", store);
}
void bintodec(int)
{
length = strlen(store)-1;
for(i=length;i>0;i--)
{
if (store[i]=='1')
{
result += pow(2,strlen(store)-i-1);
}
}
if(store[0] == '0')
{
printf("The decimal is %d", result);
}
else if(store[0] =='1')
{
leftbit = pow(2, strlen(store)-1);
/*printf("leftbit is:%d", leftbit);*/
Total = result - leftbit;
}
printf("The result is %d\n", Total);
}
Thank you!!!