Thread: puts() function doesn't return string

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    puts() function doesn't return string

    This is a program for Playfair Cipher, but after the encrypt function, puts() function is not returning the encrypted text.
    i would be glad if someone helped me out.
    The code is as follows:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    #define MAX 100
    
    void encrypt(char square[5][5],char ptext[MAX],char ctext[MAX]);
    void fillsquare(char key[MAX/4],char square[5][5]);
    int clear_input_buffer(void);
    
    void main()
    {
    	char key[MAX/4],square[5][5],ptext[MAX],ctext[MAX];
    	int ch,i,j;
    	clrscr();
    
    	do
    	{
    		printf("\nPlayfair Cipher\n");
    		printf("---------------\n");
    		printf("1. Encryption\n");
    		printf("2. Decryption\n");
    
    		printf("\nEnter your choice: ");
    		scanf("%d",&ch);
    
    		printf("Enter the key: ");
    		clear_input_buffer();
    		gets(key);
    
    		fillsquare(key,square);
    		//for(i=0;i<5;i++) for(j=0;j<5;j++) printf("%c",square[i][j]);
    		switch(ch)
    		{
    			case 1:
    				printf("\nEnter the plaintext to be encrypted: \n");
    				clear_input_buffer();
    				gets(ptext);
    
    				printf("\nstart"); puts(ptext); printf("end");
    				encrypt(square,ptext,ctext);
    
    				printf("\nCiphertext: \n");
    
    				break;
    
    			case 2:
    
    				break;
    
    			default:
    
    				break;
    		}
    
    	}
    	while(ch=='y' || ch=='Y');
    	getch();
    }
    
    int clear_input_buffer(void)
    {
    	int ch;
    	while(((ch=getchar())!=EOF) && (ch != '\n'));
    	return ch;
    }
    
    void fillsquare(char key[MAX/4],char square[5][5])
    {
    	int i,j,l=strlen(key),y=0,x,count;
    	char alpha=97;
    	for(i=0;i<5;i++)
    	{
    		for(j=0;j<5;j++)
    		{
    			if(y<l)
    			{
    				square[i][j]=key[y++];
    				//printf("%c",square[i][j]);
    			}
    			else
    			{
    				count=1;
    				while(count==1)
    				{
    					x=0,count=0;
    					while(key[x]!='\0')
    					{
    						if(key[x++]==alpha)
    						{
    							count++;
    							alpha++;
    						}
    					}
    
    				}
    				square[i][j]=alpha++;
    			}
    		}
    	}
    }
    
    void encrypt(char square[5][5],char ptext[MAX],char ctext[MAX])
    {
    	int x=0,y=0,i,j,al1row,al1col,al2row,al2col;
    
    	printf("\nout of while");
    	while(ptext[x]!='\0')
    	{
    
    		printf("\nwhile");
    		if(ptext[x]==ptext[x+1])
    		{
    
    		}
    		else
    		{
    			for(i=0;i<5;i++)
    			{
    				for(j=0;j<5;j++)
    				{
    					if(ptext[x]==square[i][j])
    					{
    						al1row=i;
    						al1col=j;
    					}
    					if(ptext[x+1]==square[i][j])
    					{
    						al2row=i;
    						al2col=j;
    					}
    				}
    			}
    			printf("\nal1row: %d al1col: %d al2row: %d al2col: %d",al1row,al1col,al2row,al2col);
    			if(al1row==al2row)
    			{
    				ctext[y++]=square[al1row][al1col+1];
    				ctext[y++]=square[al2row][al2col+1];
    			}
    			if(al1col==al2col)
    			{
    				ctext[y++]=square[al1row+1][al1col];
    				ctext[y++]=square[al2row+1][al2col];
    			}
    			if(al1row != al2row && al1col!=al2col)
    			{
    				ctext[y++]=square[al2row][al1col];
    				ctext[y++]=square[al1row][al2col];
    			}
    			x+=2;
    		}
    		ctext[y]='\0';
    	}
    }
    Last edited by kappilrinesh; 02-04-2012 at 01:25 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    puts() is not being called after doing the encryption, so you can hardly expect it to output the encrypted text.

    Also, main() returns int not void, and gets() is a recipe for a buffer overrun.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    yeah, my mistake.
    but there is a puts() function before the encrypt function and it is not returnin the ptext string on the screen.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have you considered the possibility that your call of clear_input_buffer() is discarding the input?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    its working now. i removed the call to the function clear_input_buffer().

    thank you

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Now, can you explain why that worked? I ask that because, in the process of working out the answer, you will gain some insights into how functions like gets() and getchar() work and, more importantly, how they interact when you use them together..

    You might also want to consider using fgets() (note the leading 'f') rather than gets(). Consider what will happen in your code if the user enters 200 characters without an intervening newline.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hints to get the code to work as I thought it should.
    Playfair Cipher does not normally use "J" letter instead it is replaced with "I".
    Therefore the 5x5 matrix should not have J in it.

    Note: The other logic problem is left for user to fix. Hint in body of "if(al1row != al2row && al1col!=al2col)"

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-16-2011, 07:27 AM
  2. strstr doesn't return FALSE for empty string
    By doia in forum C Programming
    Replies: 3
    Last Post: 07-12-2010, 01:02 PM
  3. function that doesn't return anything
    By izzy in forum C Programming
    Replies: 1
    Last Post: 03-19-2009, 04:40 PM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Function doesn't return an object
    By Aiwendil in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2004, 03:15 PM