Help with simple encode program

This is a discussion on Help with simple encode program within the C Programming forums, part of the General Programming Boards category; This is a simple casear cipher encode only portion that i wrote. it: 1. Promtps for user input 2. Asks ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Smile Help with simple encode program

    This is a simple casear cipher encode only portion that i wrote. it:

    1. Promtps for user input
    2. Asks for the shift value
    3. Uses the shift value such as 2 on the first letter then increments it by 1 for each letter after that.
    EXAMPLE: Hello would be Jhpqu


    My problem is at the end of each encrypted word it prints a funny character that i dont need. How can i eliminate it??thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
       
    
    
    
    /*encode function, to encode user input */
    int main()
    {
    	char buff[BUFSIZ];
    	int i = 0;
    	int shift_value;
    	int p = 0;
    
        printf( "**initializing.....**\n\n"
    	        "**linking..........**\n\n");
    	printf( "Welcome agent Brown\n\n");
    	printf( "You have accessed the Caesar Cipher program\n\n");
    	printf("\nPlease enter the text you wish to encrypt: ");/*accept user input */
    	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("\n Your encrypted text is:%s\n",buff);/*return encrypted text */
    
    	
    	return 0; /* indicate successful completeion */
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Remove the newline.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [edit] Curses, foiled again! [/edit]


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No, see my link.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    oh the wonders of being a noob. I tried adjusting the code in the link and utilizing it in my code in different parts, but now it doesnt return anything. i dont get any text back now.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
       
    
    
    
    /*encode function, to encode user input */
    int main()
    {
    	char buff[BUFSIZ];
    	int i = 0;
    	int shift_value;
    	int p = 0;
    	char *r;
    
        printf( "**initializing.....**\n\n"
    	        "**linking..........**\n\n");
    	printf( "Welcome agent Brown\n\n");
    	printf( "You have accessed the Caesar Cipher program\n\n");
    	printf("\nPlease enter the text you wish to encrypt: ");/*accept user input */
    	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++;
    				
    				 }
    		
    	}
    	if (fgets(buff, sizeof(buff), stdin) != NULL)
    	{
    		if ((r = strchr(buff, '\n')) != NULL)
    		*r = '\0';
    
    		printf("\nYour encrypted text is:%s\n",buff);/*return encrypted text */
    
    	}
    	return 0; /* indicate successful completeion */
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be removing the newline from the buffer before you shift it?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    arg, yes, and i just noticed this line i have in there.
    Code:
    while ( buff[i] != '\0'  )
    thats evidently not going to work anymore.
    How could i take that out or modify it and get the code to work?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Well, you could go until buff[i] was '\n' instead...
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    hmm this is what i have now. the character at the end from the newline is gone but now the first 2 letters are funny shapes??

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
       
    
    
    
    /*encode function, to encode user input */
    int main()
    {
    	char buff[BUFSIZ];
    	int i = 0;
    	int shift_value;
    	int p = 0;
    	char *r;
    
        printf( "**initializing.....**\n\n"
    	        "**linking..........**\n\n");
    	printf( "Welcome agent Brown\n\n");
    	printf( "You have accessed the Caesar Cipher program\n\n");
    	printf("\nPlease enter the text you wish to encrypt: ");/*accept user input */
    	fgets(buff, sizeof(buff), stdin);
    	if (fgets(buff, sizeof(buff), stdin) != NULL)
    	{
    		if ((r = strchr(buff, '\n')) != NULL)
    		*r = '\0';
    	}
    
    
    	
    	printf("\nEnter your encryption shift value (anything from +-1 to 25): "); 
        scanf ("%i", &shift_value);
    
    	{	
    			while ( buff[i] != '\n'  )
    			{
    				
    				buff[i] = buff[i] + shift_value + i++;
    				
    				 }
    		
    	}
    	
    		printf("\nYour encrypted text is:%s\n",buff);/*return encrypted text */
    
    	
    	return 0; /* indicate successful completeion */
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All you need to do is pay attention to what it is you're really doing, rather than just keep changing small seemingly random portions of your code in hope you stumble across whatever it is that really isn't working right. Look:
    Code:
    fgets(buff, sizeof(buff), stdin);
    	if (fgets(buff, sizeof(buff), stdin) != NULL)
    	{
    		if ((r = strchr(buff, '\n')) != NULL)
    		*r = '\0';
    	}
    1 - Fill the line with something.
    2 - Fill it again with something else, and then, remove the newline from that line of text.

    Why? Why are you getting two lines of text here?

    Code:
    while ( buff[ i ] != '\n'  )
    			{
    				
    				buff[ i ] = buff[ i ] + shift_value + i++;
    1 - A for loop would be better, because then you're actually initializing i right before you use it, so you don't have to wonder what's in it.
    2 - You have already removed the newline character in the code above, so why are you trying to go until you find one?

    See? All you need to do is pay attention to what it is you're actually doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > buff[i] = buff[i] + shift_value + i++;
    You'd be much better off if you didn't have undefined operations in your code either.

    Just simplify to
    buff[i] = buff[i] + shift_value + i;
    i++;

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    i do have the habit of jumping ahead of myself without paying attention. This is just so overwhelming right now. Hopefully with more practice i can not fret so much about writing these programs.

    I understand the for loop makes more sense. But what would i be checking for, i have tested a few ideas to no avail. I either get back no text, or 1 funny character.

    Code:
    	{	
    		for(??? )
    			{
    
    			  buff[i] = buff[i] + shift_value + i;
    			  i++;
    				
    			}
    		
    	}

  14. #14
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Code:
    buff[i] = buff[i] + shift_value + i;
    is unlikely to produce the result that you want.
    • you're not rotating, just "shifting" the character value up
    • the amount that you're "shifting" the character code by increases as the loop progresses
    If this is for a homework assignment, I'm sure that you've been introduced to the character type functions, or at least to the ASCII character set. If you add, say, 13 to the character "z", you get a character code that is not a letter.
    Insert obnoxious but pithy remark here

  15. #15
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Which means, in effect, that you need what is called modulo maths, which is maths modulo number of letters in your alphabet. Formula for shifting one letter:

    result = c - offset + shift mod(%) number of letters + offset

    offset is how far from 0 your alphabet numbering starts. c is your character to shift. number of letters is number of letter in alphabet.

    You subtract the offset to make it as if your numbering started at 0, add the shift value, and modulo by the number of letters to wrap is your result exceeds them (Not sure how % operator reacts if in x % y, y is bigger than x.). Then you add offset back on to get your new character.
    Last edited by Silfer; 12-09-2005 at 03:34 PM.
    -S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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