Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hi need help in 2 projects in c

    hello my friends .my first problem...i ve develop an algorith of the casear encryption but i have an error and i cant make it work..the decryption works fine but i have problem with the encryption and an error here is the error it seems right but dunoo "%s" -= "Z" - "A"; c2016.. anyone could help..

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void menu ( void ) {
       printf("Please enter a number: \n"
              "1-Encrypt\n"
              "2-Decrypt\n"
              "3-Exit\n"
              "prompt > ");
       fflush( stdout );
    }
    
    int getchoice ( void ) {
       char buff[BUFSIZ];
       int  choice = 0;
       do {
           menu();
           if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
               /* success reading a line, does it make sense? */
               if ( sscanf( buff, "%d", &choice ) != 1 ) {
                   printf( "Enter a number\n" );
               }
           } else {
               /* user EOF, just exit now */
               choice = 3;
           }
       } while ( choice < 1 || choice > 3 );
       return choice;
    }
    
    void encode ( void ) 
    {
        char buff[BUFSIZ];
        int i = 0;
        int shift_value;
         printf( "Doing encrypt\n" );
        printf("\nPlease enter the text you wish to encrypt: ");
        fgets(buff, sizeof(buff), stdin);
            if ("%s"  > 'Z')
        {
           "%s" -= 'Z' - 'A';
        }
        printf("\nEnter your encryption shift value (anything from +-1 to 25): "); 
       scanf ("%i", &shift_value);
        
        
    
        {    
     while ( buff[i] != '\0' )
         {
       buff[i] = buff[i] + shift_value;
       i++; 
       
         }
        }
     printf("\n Your encrypted text is: %s \n", buff);
    }
    void decode ( void ) 
    {
        char buff[BUFSIZ];
        int i = 0;
        int shift_value;
                   printf( "Doing decrypt\n" );
        printf("\nPlease enter the text you wish to decrypt: ");
        fgets(buff, sizeof(buff), stdin);
        printf("\nEnter your encryption shift value (anything from +-1 to 25): "); 
       scanf ("%i", &shift_value);
    
        {
     while ( buff[i] != '\0' ) 
         {
       buff[i] = buff[i] - shift_value;
       i++;
         }
        }
     printf("\nYour decrypted text is: %s \n", buff);
    }
    int main ( ) {
       int choice;
        while ( (choice=getchoice()) != 3 ) {
           if ( choice == 1 ) {
    
               encode();
           } else
           if ( choice == 2 ) {
               decode();
           }
       }
     
    
       return 0;
    }
    well thats my first problem my second is that i have made 2 separate codes for the affine algorith encryption decryption but whenever i try to make them under a case or if so i can have a simple menu like 1) encryption 2)decryption it always dont work...((

    here is the code for encryption
    Code:
    #include<stdio.h>
    #include<string.h>
    void main()
    {
    	int ch,a1,b1,a,b,c,d,d1[15],h,n,i=0;
    	char str1[80],ek[80];
    	printf("Dwse to keimeno\n");
    	gets(str1);
      	do
    	{
    	printf("Dwse to kleidi\na:");
    	scanf("%d",&a);
    	printf("\nb:");
    	scanf("%d",&b);
    	a1=a;
    	b1=b;
    	b=26;
    	d=1;
    	while(d!=0)
    	{
    		
    		
    			c=a/b;
    			
    			d=a%b;
    			d1[i]=d;
    			a=b;
    			b=d;
    		i=i++;
    		
    		
    
    	}
    	
    	n=i-1;
    
    	
    	
    	
    
    			if(d1[n-1]==1)
    			{
    				printf("\nOi ariumoi einai prvtoi metaji tous\n\n");
    				ch=1;
    
    			}
    			else 
    			{
    				printf("\nOi ariumoi den einai prvtoi metaji tous\n\n");
    				ch=0;
    			}
    	}
    	while(ch==0);
    			a=a1;
    			b=b1;
    			printf("\nTo kleidi einai (%d,%d):\n",a,b);
    
    			printf("\nTo keimeno einai %s  to mikos toy %d	\n",str1,strlen(str1));
    			n=strlen(str1);
    		
    			ek[n]='\0';
    			h=strlen(ek);
    			for (i=0; i<h; i++)
    			{
    				str1[i]=str1[i]-97;
    				ek[i]=((a*str1[i]+b)%26)+97;
    				printf("%c -> %d to \n",str1[i]+97,str1[i]);
    			}
    			
    			printf("\n\nTo kriptografhmeno keimeno einai:%s \n",ek);
    			for (i=0; i<h; i++)
    			{
    				printf("%c -> %d \n",ek[i],ek[i]-97);
    			}
    			printf("To kriptografhmeno keimeno einai:%s \n\n",ek);
    			
    }
    and here is for the decription ..any ideas how this can be made in one single code with the simple if or case menu..
    Code:
    #include<stdio.h>
    #include<string.h>
    void mul(int a1,int b1,int *d,int *x,int *y);
    void main()
    {
    	int ch,xx,a1,b1,a,b,c,d,d1[15],h,n,x,key,y,i=0;
    	char str1[80],dk[80];
    	printf("Dwse to keimeno\n");
    	gets(str1);
      	do
    	{
    	printf("Dwse to kleidi\na:");
    	scanf("%d",&a);
    	printf("\nb:");
    	scanf("%d",&b);
    
    	a1=a;
    	b1=b;
    	b=26;
    	mul(a,b,&d,&x,&y);
    	key=x;
    	if (key<0)
    	{
    		key=26+key;
    	}
    	printf("\n1/a:%d \n",key);
    
    	d=1;
    	while(d!=0)
    	{
    		
    		
    			c=a/b;
    			
    			d=a%b;
    			d1[i]=d;
    			a=b;
    			b=d;
    		i=i++;
    		
    		
    
    	}
    	
    	n=i-1;
    
    	
    	
    	
    
    			if(d1[n-1]==1)
    			{
    				printf("\nOi ariumoi einai prvtoi metaji tous\n\n");
    				ch=1;
    
    			}
    			else 
    			{
    				printf("\nOi ariumoi den einai prvtoi metaji tous\n\n");
    				ch=0;
    			}
    	}
    	while(ch==0);
    			a=a1;
    			b=b1;
    			printf("\nTo kleidi einai (%d,%d):\n",a,b);
    
    			printf("\nTo keimeno einai %s  to mikos toy %d	\n",str1,strlen(str1));
    			n=strlen(str1);
    		
    			dk[n]='\0';
    			h=strlen(dk);
    			for (i=0; i<h; i++)
    			{
    				str1[i]=str1[i]-97;
    				xx=(str1[i]-b)%26;/*to kano se periptosi pou o b einai megalos*/
    				if (xx<0)
    				{
    					xx=26+xx;
    				}
    				
    				dk[i]=((key*(xx))%26)+97;
    				printf("%c -> %d\n",str1[i]+97,str1[i]);
    			}
    			printf("\n\nTo kriptografhmeno keimeno einai:%s \n",dk);
    			for (i=0; i<h; i++)
    			{
    				printf("%c -> %d\n",dk[i],dk[i]-97);
    			}
    			printf("To kriptografhmeno keimeno einai:%s \n\n",dk);
    			
    }
    
    void mul(int a1,int b1,int *d1,int *x1,int *y1)
    {int c,d,x,y;
    	if(b1==0)
    	{
    		*d1=a1;
    		*x1=1;
    		*y1=0;
    		
    	
    	}
    	else{
    	d=*d1;
    	x=*x1;
    	y=*y1;
    		
    
    
    c=a1%b1;
    
    mul(b1,c,&d,&x,&y);
    	
    
    *d1=d;
    *x1=y;
    *y1=x-(a1/b1)*(y);
    
    	}
    	
    }
    thank you for the time you gonna spend in helping me..)
    ps the text in the printf is in greek as i m from greece
    :gOOd:
    Last edited by oracleguy; 11-21-2008 at 06:18 PM. Reason: Added code tags

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    My suggestion would be to run this through a debugger and step through the code until you get to the error. What you see in the debugger will help in figuring out what is causing it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: hi need help in 2 projects in c

    Hi,

    Please send me those errors. I checked it and reply to you.

  • #4
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post
    "%s" -= "Z" - "A"; in your post is not the same as in your code where you have "%s" -= 'Z' - 'A';
    now your problem is likely to be with all the literals in that. basically what you are trying to do is say to the computer
    'set the value of 45 = 45 - 12 - 10' as you can see this is nonsense, hence your computer is complaining! are you sure you are trying to set the value of "%s" (a literal NOT a varible) to the value of itself -Z, -A?

    I don't know how much experience you have, but I think if your struggling with this you might need to start with something simpler.

    something like:
    "hello %s" is seen to the computer as an array of numbers (chars) ie [h],[e],[l],[l],[o],[ ],[%],[s] the %s is just a place holder the function printf notices the %s and replaces it with the argument passed.

    I would make an attempt to solve your problems, however your code is really too messed up to fix and would be better re-writing. learn again about variables, strings and try again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •