Enter KEY

This is a discussion on Enter KEY within the C Programming forums, part of the General Programming Boards category; Hi All, Need help with my code. doesn't work at all it give me either no result & no error ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    Enter KEY

    Hi All,
    Need help with my code. doesn't work at all it give me either no result & no error is showing i am using Borland C5
    also this version is not working
    how i can fix my code? where i can find Borland C5.5 Dolphi?====================
    Here's my code
    ////// can somone help me with this?
    //////
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void cc(char sen[]);
    void main()
     {
    
    char sen[20];
    int j,i,w=0;
    	printf("  enter the plaintext:  ");
       for(i=0;i<20;i++)
    	scanf("%s",sen[i]);
    
    	for(j=0;j<strlen(sen);j++)
       if(sen[j]==' '|| sen[j]!=' ')
    	{  w++;
        }
    
     for(i=0;i<w;i++)
     cc(sen);
    
     getch();
     }
    
    void cc(char sen[])
      {
        FILE *fp ;
    
      int i,j,k1[8],ls_2[10],k2[8],ls_1[10];
      int p10[10],key[10] ;
      int pt[8],IP[8],EP[8];
      int a[4];
      int b[8];
      int c[8];
      int temp[8];
      char s[5];
      int p[5],bin[8],div[8];
    
        printf("Enter KEY= ");
      for(i=0;i<10;i++)
      scanf("%d",&key[i]);
    
    
         p10[0]=key[2];
         p10[1]=key[4];
         p10[2]=key[1];
         p10[3]=key[6];
         p10[4]=key[3];
         p10[5]=key[9];
         p10[6]=key[0];
         p10[7]=key[8];
         p10[8]=key[7];
         p10[9]=key[5];
    
    
    
          ls_1[4]=p10[0];
         for(j=0;j<4;j++)
         ls_1[j]=p10[j+1];
    
          ls_1[9]=p10[5];
         for(j=5;j<9;j++)
          ls_1[j]=p10[j+1];
    
    
         k1[0]=ls_1[5];
         k1[1]=ls_1[2];
         k1[2]=ls_1[6];
         k1[3]=ls_1[3];
         k1[4]=ls_1[7];
         k1[5]=ls_1[4];
         k1[6]=ls_1[9];
         k1[7]=ls_1[8];
    
    
    
         ls_2[4]=ls_1[0];
         ls_2[3]=ls_1[1];
        for (j=0;j<3;j++)
         ls_2[j]=ls_1[j+2];
    
          ls_2[9]=ls_1[5];
          ls_2[8]=ls_1[6];
         for(j=5;j<8;j++)
         ls_2[j]=ls_1[j+2];
    
    
         k2[0]=ls_2[5];
         k2[1]=ls_2[2];
         k2[2]=ls_2[6];
         k2[3]=ls_2[3];
         k2[4]=ls_2[7];
         k2[5]=ls_2[4];
         k2[6]=ls_2[9];
         k2[7]=ls_2[8];
    
       printf("Enter plaintext= ");
      for(i=0;i<20;i++)
      s[i]=sen[i];
    
      for(i=0;i<1;i++)
       printf("\n ASCII= %d",s[i]);
    
      for(i=0;i<1;i++)
       p[i]=s[i];
    
      int k[1];
    
        div[0]=p[0]/2;
        k[0]=p[0]%2;
    
         for(i=0;i<8;)
        {
          if(div[i]>=0)
          {
           div[i+1]=div[i]/2;
           bin[i+1]=div[i]%2;
          }
          else if(i==6)
           bin[i+1]=0;
    
         i++;
        };
    
       temp[0]=bin[7];
       temp[1]=bin[6];
       temp[2]=bin[5];
       temp[3]=bin[4];
       temp[4]=bin[3];
       temp[5]=bin[2];
       temp[6]=bin[1];
       temp[7]=k[0];
    
    
      for(i=0;i<8;i++)
      pt[i]=temp[i];
    
         IP[0]=pt[1];
         IP[1]=pt[5];
         IP[2]=pt[2];
         IP[3]=pt[0];
         IP[4]=pt[3];
         IP[5]=pt[7];
         IP[6]=pt[4];
         IP[7]=pt[6];
    
    
        for(i=0,j=4;i<4;i++,j++)
          a[i]=IP[j];
    
    
         EP[0]=a[3];
         EP[1]=a[0];
         EP[2]=a[1];
         EP[3]=a[2];
         EP[4]=a[1];
         EP[5]=a[2];
         EP[6]=a[3];
         EP[7]=a[0];
    
    
        for(i=0;i<8;i++)
          b[i]=EP[i];
         for(i=0;i<8;i++)
         c[i]=b[i]^k1[i];
    
    
        int s0[2],s1[2];
    
        if((c[0]==0&&c[3]==0)&&(c[1]==0&&c[2]==0))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((c[0]==0&&c[3]==1)&&(c[1]==0&&c[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((c[0]==0&&c[3]==0)&&(c[1]==0&&c[2]==1))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((c[0]==0&&c[3]==1)&&(c[1]==0&&c[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((c[0]==0&&c[3]==0)&&(c[1]==1&&c[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((c[0]==0&&c[3]==0)&&(c[1]==1&&c[2]==1))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((c[0]==0&&c[3]==0)&&(c[1]==1&&c[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((c[0]==0&&c[3]==1)&&(c[1]==1&&c[2]==1))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((c[0]==1&&c[3]==0)&&(c[1]==0&&c[2]==0))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((c[0]==1&&c[3]==1)&&(c[1]==0&&c[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((c[0]==1&&c[3]==0)&&(c[1]==0&&c[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((c[0]==1&&c[3]==1)&&(c[1]==0&&c[2]==1))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((c[0]==1&&c[3]==0)&&(c[1]==1&&c[2]==0))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((c[0]==1&&c[3]==1)&&(c[1]==1&&c[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((c[0]==1&&c[3]==0)&&(c[1]==1&&c[2]==1))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((c[0]==1&&c[3]==1)&&(c[1]==1&&c[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
    
        if((c[4]==0&&c[7]==0)&&(c[5]==0&&c[6]==0))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((c[4]==0&&c[7]==1)&&(c[5]==0&&c[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((c[4]==0&&c[7]==0)&&(c[5]==0&&c[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((c[4]==0&&c[7]==1)&&(c[5]==0&&c[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((c[4]==0&&c[7]==0)&&(c[5]==1&&c[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((c[4]==0&&c[7]==0)&&(c[5]==1&&c[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((c[4]==0&&c[7]==0)&&(c[5]==1&&c[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((c[4]==0&&c[7]==1)&&(c[5]==1&&c[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((c[4]==1&&c[7]==0)&&(c[5]==0&&c[6]==0))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((c[4]==1&&c[7]==1)&&(c[5]==0&&c[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((c[4]==1&&c[7]==0)&&(c[5]==0&&c[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((c[4]==1&&c[7]==1)&&(c[5]==0&&c[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((c[4]==1&&c[7]==0)&&(c[5]==1&&c[6]==0))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((c[4]==1&&c[7]==1)&&(c[5]==1&&c[6]==0))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((c[4]==1&&c[7]==0)&&(c[5]==1&&c[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((c[4]==1&&c[7]==1)&&(c[5]==1&&c[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
    
        int t[4],x[4],P4[4];
        t[0]=s0[0];
        t[1]=s0[1];
        t[2]=s1[0];
        t[3]=s1[1];
    
        P4[0]=t[1];
        P4[1]=t[3];
        P4[2]=t[2];
        P4[3]=t[0];
    
    
        for(i=0;i<4;i++)
         x[i]=IP[i]^P4[i];
    
    
         b[0]=x[3];
         b[1]=x[0];
         b[2]=x[1];
         b[3]=x[2];
         b[4]=x[1];
         b[5]=x[2];
         b[6]=x[3];
         b[7]=x[0];
    
         int y[8];
    
         for(i=0;i<8;i++)
          y[i]=b[i]^k2[i];
    
    
        if((y[0]==0&&y[3]==0)&&(y[1]==0&&y[2]==0))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((y[0]==0&&y[3]==1)&&(y[1]==0&&y[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((y[0]==0&&y[3]==0)&&(y[1]==0&&y[2]==1))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((y[0]==0&&y[3]==1)&&(y[1]==0&&y[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((y[0]==0&&y[3]==0)&&(y[1]==1&&y[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((y[0]==0&&y[3]==0)&&(y[1]==1&&y[2]==1))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((y[0]==0&&y[3]==0)&&(y[1]==1&&y[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((y[0]==0&&y[3]==1)&&(y[1]==1&&y[2]==1))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((y[0]==1&&y[3]==0)&&(y[1]==0&&y[2]==0))
           {  s0[0]=0;
              s0[1]=0;
           }
        else if ((y[0]==1&&y[3]==1)&&(y[1]==0&&y[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((y[0]==1&&y[3]==0)&&(y[1]==0&&y[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
        else if ((y[0]==1&&y[3]==1)&&(y[1]==0&&y[2]==1))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((y[0]==1&&y[3]==0)&&(y[1]==1&&y[2]==0))
           {  s0[0]=0;
              s0[1]=1;
           }
        else if ((y[0]==1&&y[3]==1)&&(y[1]==1&&y[2]==0))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((y[0]==1&&y[3]==0)&&(y[1]==1&&y[2]==1))
           {  s0[0]=1;
              s0[1]=1;
           }
        else if ((y[0]==1&&y[3]==1)&&(y[1]==1&&y[2]==1))
           {  s0[0]=1;
              s0[1]=0;
           }
    
    
        if((y[4]==0&&y[7]==0)&&(y[5]==0&&y[6]==0))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((y[4]==0&&y[7]==1)&&(y[5]==0&&y[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((y[4]==0&&y[7]==0)&&(y[5]==0&&y[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((y[4]==0&&y[7]==1)&&(y[5]==0&&y[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((y[4]==0&&y[7]==0)&&(y[5]==1&&y[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((y[4]==0&&y[7]==0)&&(y[5]==1&&y[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((y[4]==0&&y[7]==0)&&(y[5]==1&&y[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((y[4]==0&&y[7]==1)&&(y[5]==1&&y[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((y[4]==1&&y[7]==0)&&(y[5]==0&&y[6]==0))
           {  s1[0]=1;
              s1[1]=1;
           }
        else if ((y[4]==1&&y[7]==1)&&(y[5]==0&&y[6]==0))
           {  s1[0]=1;
              s1[1]=0;
           }
        else if ((y[4]==1&&y[7]==0)&&(y[5]==0&&y[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((y[4]==1&&y[7]==1)&&(y[5]==0&&y[6]==1))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((y[4]==1&&y[7]==0)&&(y[5]==1&&y[6]==0))
           {  s1[0]=0;
              s1[1]=1;
           }
        else if ((y[4]==1&&y[7]==1)&&(y[5]==1&&y[6]==0))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((y[4]==1&&y[7]==0)&&(y[5]==1&&y[6]==1))
           {  s1[0]=0;
              s1[1]=0;
           }
        else if ((y[4]==1&&y[7]==1)&&(y[5]==1&&y[6]==1))
           {  s1[0]=1;
              s1[1]=1;
           }
    
        t[0]=s0[0];
        t[1]=s0[1];
        t[2]=s1[0];
        t[3]=s1[1];
    
        P4[0]=t[1];
        P4[1]=t[3];
        P4[2]=t[2];
        P4[3]=t[0];
    
    
        int z[4];
        for(i=0,j=4;i<4;i++,j++)
         z[i]=IP[j]^P4[i];
    
    
        b[0]=z[0];
        b[1]=z[1];
        b[2]=z[2];
        b[3]=z[3];
        b[4]=x[0];
        b[5]=x[1];
        b[6]=x[2];
        b[7]=x[3];
    
        int IP_1[8];
    
        IP_1[0]=b[3];
        IP_1[1]=b[0];
        IP_1[2]=b[2];
        IP_1[3]=b[4];
        IP_1[4]=b[6];
        IP_1[5]=b[1];
        IP_1[6]=b[7];
        IP_1[7]=b[5];
    
       int R[8],f;
       j=0;
      while(j<8)
      {
         if(j==0)
         R[j]=IP_1[j]*1;
    
        else  if(j>0&&j<8)
         switch(j)
         {
          case 1:R[j]=IP_1[j]*2;
          break;
          case 2:R[j]=IP_1[j]*2*2;
          break;
          case 3:R[j]=IP_1[j]*2*2*2;
          break;
          case 4:R[j]=IP_1[j]*2*2*2*2;
          break;
          case 5:R[j]=IP_1[j]*2*2*2*2*2;
          break;
          case 6:R[j]=IP_1[j]*2*2*2*2*2*2;
          break;
          case 7:R[j]=IP_1[j]*2*2*2*2*2*2*2;
          break;
          }
         j++;
         };
    
      f=0;
      for(i=0;i<8;i++)
      f+=R[i];
    
       printf("\n\n ASCII= ");
       printf("%d ",f);
    
       printf("\n\n The Ciphertext= ");
       printf("%c",f);
    
           if((fp = fopen("DES.txt", "w"))==NULL)
           printf("file could not be opend");
           else{
            fprintf(fp," %c",f);
    
            fclose(fp);
            }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    char sen[20];
    int j,i,w=0;
    	printf("  enter the plaintext:  ");
            for(i=0;i<20;i++)
    	     scanf("%s",sen[i]);                <- %s should be %c?...use fgets (check faq)
    
    	for(j=0;j<strlen(sen);j++)
                  if(sen[j]==' '|| sen[j]!=' ')      <-always true why not just w = strlen(sen) - 1;
    	{  w++;
        }
    
     for(i=0;i<w;i++)         <- why call the function w times with the same input
     cc(sen);
    
     getch();
    cc must be the longest, most complicated looking function I've ever seen.

    maybe you should break it up into smaller functions
    Last edited by sl4nted; 12-03-2006 at 04:39 PM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    Wink

    Hi,

    thank you for your replay
    my code either when run hang up my PC or give no result??

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I doubt anyone is gonna try to debug that crazy function for you, I'm sure not. Break it up into smaller tasks, so you can find out where its messing up. Have you run it in a debugger to see where the problem may be atleast?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Hi Again,
    how i can break up & make my Script Samller as you mention??,
    if you don't mind can you help my with this code to make work??
    not yet i didn't

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
            for(i=0;i<20;i++)
    	     scanf("%s",sen[i]);
    first off scanf("%c", &sen[i]);

    is what I believe your trying for here, but the way this is setup, it only takes a string of 20 chars
    no more no less. try using the fgets example in the faq

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Ohh man, what the hell is that code suppose to do. Post only the part of code which you might think error is. more over u are using some non standard library function which not acceptable. Is that your teacher thought that the main return of type void???

    If it is so please change it to int. main should always return a value. It is the only way you are letting the operating system to know that program has terminates success or failure.

    ssharish2005

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    how to change it to INT?
    more explanation please?
    can you help me if you send the full code do the change it for me & run to say if its work i believe you have good experience with it

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Lool at the FAQ for more explanation. look at this sample code.

    Code:
    int main()
    {
        ----
        ----
    
        return 0;
    }
    This is how the main fucntion should look like

    I am not here to do u;r assignment, I can guide you thats it. No body is going to go through that just a hung code

    ssharish2005

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Ok Man Thank you<<>>

    i 'll check it out & let you know

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if it is C - move all your declarations like
    int z[4];
    to the beginning of the function. Standard C before C'99 does not allow mix of the declarations and regular statements.

    PS. Or even better -each part that begins with your declaration of the new variable - till the last place where this variable is used - make another function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Hi,
    now i tried again ut give this error
    Build
    [C++ Warning] code.cpp(15): W8012 Comparing signed and unsigned values

    for(i=0;i<20;i++)
    scanf("%s",sen[i]);

    what's wrong with that it drive me carzy?

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Are you sure it is a line?
    in your original code line 15 is something else...

    warning says that you somparing values of different types like int and unsigned char or int and unsigned int
    It can be potentially dangerous, so the compiler gives a warning
    If you are sure what you are doing - cast one of the values to the type of the other value explicitely
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    for(i=0;i<20;i++)
    scanf("%s",sen[i]);
    #include <stdio.h>
    int main(){
    char sen[20];
    int i=0;
    for(i=0; i<20; i++){
    scanf("%c",&sen[i]);
    }
    while (i<=20 && i>=0){
    printf("%c\n",sen[i]);
    i--;
    }
    return 0;
    }

    output:
    abcdefghijklmnopqrst

    t
    s
    r
    q
    p
    o
    n
    m
    l
    k
    j
    i
    h
    g
    f
    e
    d
    c
    b
    a

    not sure if this is what u mean... i just went through the small part of your code where you mentioned bout the warning part

  15. #15
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    It might work have you done it before !!!! if not then try it before to post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 06:45 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Dialog Edit Control and Enter Key
    By Quantrizi in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2004, 08:59 AM
  4. Need to capture the enter key
    By tyouk in forum Windows Programming
    Replies: 7
    Last Post: 11-08-2003, 06:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21