# Thread: My code does not work properly. I need help.

1. ## My code does not work properly. I need help.

Hi, My code does an xor operation for each 8 bits and stores the value in an integer variable. I get the text in char by using a while loop then I do the xor operation inside the loop for each 8 bits and store it to an integer variable. This program works perfectly for 8 bits however if it is a 16 bit code I doesn't work. I made an counter variable to check each 8 bits I reset the counter when it is 9. This should work I tried everything. If I enter more than 8 bits the integer variable that I use to store becomes a big negative integer. Sorry for the bad english. I appriciate any help and tips.

Code:
```      if (base==2)                        /*Start of the base 2 text decode*/
{
printf("Please enter the text to decode:");
scanf(" %c", &text);
while (text!=LF)                             /* This while loops takes the text from the user*/
{

if( (text!='1') && (text!='0'))
{
printf("The text is not in base 2!"); /* This if statement gives error if the text is not in base 2 and stops the program*/
break;
}
/* This huge if else chain does the xor operation and stores it in an integer value*/
if (counter==1)
{
if(code1!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if (code1==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==2)
{
if(code2!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if(code2==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==3)
{
if(code3!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if (code3==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==4)
{
if(code4!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if(code4==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==5)
{
if(code5!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if(code5==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==6)
{
if(code6!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if(code6==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==7)
{
if (code7!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if (code7==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
else if (counter==8)
{
if (code8!=text)
{
nxor=nxor+1;
nxor=nxor*10;
}
else if (code8==text)
{
nxor=nxor+0;
nxor=nxor*10;
}
}
scanf("%c", &text);
counter++;
if (counter==9)
counter=1;
totalcounter=totalcounter+1;
}```

2. > /* This huge if else chain does the xor operation and stores it in an integer value*/
Code:
```          if (counter==1)
{
if(code1!=text)```
And if you know what an array is, then you can do
Code:
`if ( code[counter] != text )`
then 8 lots of copy/paste just instantly vanish and you have a much smaller (but also more flexible) program.

3. The code you've shown can be rewritten as shown below.
Instead of a bunch of numbered variables (code1, code2, etc.) use an array; but remember that array indices start at 0 and go to one-less-than the array size (code[0], code[1], etc.).
I'm not sure what your question is, but maybe this will go some way to solving your problem, which seems to have something to do with making the code work with sizes other than 8.
Code:
```if (base == 2) // base 2 text decode
{
printf("Enter the text to decode:");
scanf(" %c", &text);
while (text != LF)
{
if (text != '1' && text != '0')
{
printf("The text is not in base 2!\n");
break;
}
if (code[counter] != text)
++nxor;
nxor *= 10;

counter = (counter + 1) % 8;
++totalcounter;

scanf("%c", &text);
}
}```