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

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    11

    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*/
                        menu_selection=3;
                        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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > /* 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    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");
                menu_selection = 3;
                break;
            }
            if (code[counter] != text)
                ++nxor;
            nxor *= 10;
     
            counter = (counter + 1) % 8;
            ++totalcounter;
     
            scanf("%c", &text);
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot get strstr to work properly
    By psppb in forum C Programming
    Replies: 3
    Last Post: 08-27-2014, 09:27 AM
  2. qsort() won't work properly..
    By gevni in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 12:02 PM
  3. don't understand why this code doesn't work properly
    By artistunknown in forum C Programming
    Replies: 10
    Last Post: 01-23-2010, 08:48 PM
  4. trying to get a few functions to work properly...
    By lemonwaffles in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2009, 11:00 PM
  5. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM

Tags for this Thread