I am having trouble with making my program read in binary and convert it into an integer so that I can do arithmetic on it, and then output it back out as binary.

Here is my code. The first part of it works, and I can read in the 2 binary numbers, but when I have a problem with the segment that converts it into an integer.

#include <stdio.h>
#include <string.h>


int main()

{
int i, num1, num2, num3=0;
unsigned choice, string;
char str1[7], op, str2[8], str4[8];
int str3[8]={64,32,16,8,4,2,1};
    

    do
    {
    system("clear");
    printf("Enter an Operation?\n");
    printf("Yes(1) or No(2)\n");
    scanf("%u", &choice);
    }while (choice < 1 || choice > 2);
{
switch(choice)

 
case 1:
{

printf("Enter two binary numbers seperated by an operator: ");
scanf("%7s%c%8s", str1, &op, str2);

    for (i==0; i<=7; i++)             //this is where the problem is.
    scanf("%d", &str3[i]);
    
    if(str1[i]=1);
    num1==(num1+str3[i]);
    if(str1[i]=0);
    num1==(num1 + 0);
    if (str2[i]=1);
    num2==(num2+str3[i]);
    if(str2[i]=0);
    num2==(num2+0);

if (op=='+')
num3==(num1+num2);
if(op=='-')
num3==(num1-num2);
if(op=='*')
num3==(num1*num2);
if(op=='/')
num3==(num1/num2);

    for (i=0; i<=7; i++)
    str4[i]=(num3 % 2);
    

system("clear");
printf("%s%c%s", str1, op, str2);
printf("\nequals %d", num3); 
break;

    case 2:
        printf("goodbye");
        
while(choice !=2);
return 0;
}
}
}

There are tons of syntax errors here. Did you try to compile your code?
1.You have not #included stdlib.h file(system() is defined in stdlib.h).
Are you using that to clear the screen? I think you should avoid doing that.As some people say, you have no right to own the console.
2.In your for statement.

for (i==0; i<=7; i++) //this is where the problem is

'==" is not an assignment operator.It compares the left and right operands for equality.
it should be

for(i=0;i<=7;i++);

The same thing applies to your 'if' statements too.
3.In line 32

scanf("%d", &str3[i]);

You have already initialized the array str3

int str3[8]={64,32,16,8,4,2,1};

and again you're trying to save a(God knows what) value to each of them.

scanf("%d", &str3[i]);

I hope this will help.

Thanks for the tips. I have gotten it to compile, and have changed all of the things you said. The piece of code that I pointed out is supposed to look for a 1 in that position, and if so it adds that number to an integer, and at the end it should equal the binary number as an integer. That is why str3 is set to have those numbers in it. I still have problems once I compile though, because somewhere my code is not doing the right math. I get an answer, but it is not what it should be. After the changes you suggested it looks like this.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()

{
int i, num1, num2, num3=0;
unsigned choice, string;
char str1[7], op, str2[8], str4[8];
int str3[8]={64,32,16,8,4,2,1};
	

	do
	{
	system("clear");
	printf("Enter an Operation?\n");
	printf("Yes(1) or No(2)\n");
	scanf("%u", &choice);
	}while (choice < 1 || choice > 2);
{
switch(choice)

 
case 1:
{

printf("Enter two binary numbers seperated by an operator: ");
scanf("%7s%c%8s", str1, &op, str2);

	for (i=0; i<=7; i++)
	
	if(str1[i]=='1');            //This is where there is problems 
	num1=(num1+str3[i]);        //Convert Binary into integer so i can do arithmetic
	if(str1[i]=='0');
	num1=(num1 + 0);
	

        if (str2[i]=='1');
	num2=(num2+str3[i]);
	if(str2[i]=='0');
	num2=(num2+0);



if (op=='+')
num3=(num1+num2);          //do the arithmetic on the converted binary number
if(op=='-')
num3=(num1-num2);
if(op=='*')
num3=(num1*num2);
if(op=='/')
num3=(num1/num2);

	
for (i=0; i<=7; i++)           //convert it back into binary
str4[i]=(num3 % 2);
	

system("clear");
printf("%s%c%s", str1, op, str2);
printf("\nequals %s", str4); 
break;

	case 2:
		printf("goodbye");
		
while(choice !=2);
return 0;
}
}
}

It composes for me but the outcome is not even close to correct. I am only using the system() to clear the screen so that it will clean it up a little so that it is a little easier for the users to read.

if(str1[i]=='1');

This code is read by the compiler like

if(str1[i]== '1')
    ;//[I]Do Nothing [/I]
num1=(num1 + 0);//[I]outside the branch condition [/I]

and also

for (i=0; i<=7; i++)
 
if(str1[i]=='1'); //This is where there is problems
num1=(num1+str3[i]); //Convert Binary into integer so i can do arithmetic
if(str1[i]=='0');
num1=(num1 + 0);
 
 
if (str2[i]=='1');
num2=(num2+str3[i]);
if(str2[i]=='0');
num2=(num2+0);

the for loop statements should have curly braces if they have body with multiple statements.

for(i=0;i<=7;i++){
    /* loop body */
}

Same rule goes with your switch statement

switch(expression){
    case 1:
    /*do something*/
    break;

    
    case 2:
    /*do something*/
    break;
}

I think you should go through the first few chapters(upto the discussion of the loop statements) of your favorite C Programming book and then come back later and try to deal with this code.
There are some issues with your algorithm too but we'll deal with them later.

I have gotten down to the final portion of the program. Now i just need the program to convert the number back into binary. This is my code. The highlighted part is where i seem to be encountering trouble.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()

{
int i, num1, num2, num3;
unsigned choice, string;
char str1[7], op, str2[8], str4[7];
int str3[7]={64,32,16,8,4,2,1};
	

	do
	{
	system("clear");
	printf("Enter an Operation?\n");
	printf("Yes(1) or No(2)\n");
	scanf("%u", &choice);
	}while (choice < 1 || choice > 2);
{
switch(choice)
 
case 1:
{

printf("Enter the Operation: ");
scanf("%7s%c%8s", str1, &op, str2);
num1=0;

for (i=0; i<7; i++)	
	if(str1[i]=='1')
	    num1=num1+str3[i];

printf("\nNum1 is: %d\n", num1);
num2=0;	

for (i=0; i<7; i++)	
	if (str2[i]=='1')
	    num2=(num2+str3[i]);
	
	
printf("\nNum2 is: %d\n", num2);
num3=0;

	
	if (op=='+')
	num3=(num1+num2);

	if(op=='-')
	num3=(num1-num2);
	
	if(op=='*')
	num3=(num1*num2);
	
	if(op=='/')
	num3=(num1/num2);

printf("\nnum3 is: %d\n", num3);
	
for (i=6; i>=0; i--){
	str4[i]=num3 % 2;                  //this is where the program doesn't seem to work out
	 (num3=num3 / 2);	
	}


printf("%s%c%s", str1, op, str2);
printf("\n= %s\n", str4); 
break;

	case 2:
		printf("goodbye");
		
while(choice !=2);
return 0;
}
}
}

The output will not show the remainder of the modulus 2 i did on num3, and it is not populating str4.

I have used getchar() to take the input instead of scanf beacause it looked very messy and probably is unsafe too(the way you're handling the whole array with the magical scanf() and it's %s format specifier).
I've also used a function to keep the switch statement simple and more readable. exit() is just a function defined in stdlib.h library to exit the program right away.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void binop(void);



int main()

{

unsigned choice;

	do
	{
	printf("Enter an Operation?\n");
	printf("Yes(1) or No(2)\n");
	scanf("%u", &choice);
	}while (choice < 1 || choice > 2);

switch(choice){
	case 1: //case 1
		binop();
		break;



	case 2://case 2
	printf("goodbye");
	break;
}

return 0;

}

void binop(void)
{
	int i, num1=0, num2=0, num3=0,sum=0;
	int str1[8],op, str2[8], str4[8];
	int str3[8]={128,64,32,16,8,4,2,1},c;

	printf("Enter the first binary number: ");
	fflush(stdin);
	for(i=0;i<8;i++){
		c=getchar();
		if(c>'1' || c<'0')
			exit(1);//not binary , handle anyway you like
		str1[i]=c-48;
	}

	printf("Enter the next binary number: ");
	fflush(stdin);

	for(i=0;i<8;i++){
		c=getchar();
		if(c>'1' || c<'0')
			exit(1);//not binary , handle anyway you like
		str2[i]=c-48;//convert to ascii
	}

	printf("enter the operation to perform:");
	fflush(stdin);
	op=getchar();



	for (i=0;i<8; i++){
		num1=(str1[i]*str3[i])+num1;//Convert Binary into integer so i can do arithmetic

		num2=str2[i]*str3[i]+num2;
	}

		if (op=='+')
			num3=(num1+num2);//do the arithmetic on the converted binary number
		if(op=='-')
			num3=(num1-num2);
		if(op=='*')
			num3=(num1*num2);
		if(op=='/')
			num3=(num1/num2);
       printf("%d in decimal\n",num3);




	for(i=7;i>-1;i--){//convert it back into binary
            if(i-1==0 && (num3/2)>1){
                printf("overflow!\n");
                exit(1);
            }
            str4[i]=num3%2;
            num3=num3/2;
	}
	for(i=0;i<8;i++)
            printf("%d",str4[i]);

}
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.