Need help assignment

This is a discussion on Need help assignment within the C Programming forums, part of the General Programming Boards category; The aim of this program is to read a message on a file using fscanf and print it to another ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    Need help assignment

    The aim of this program is to read a message on a file using fscanf and print it to another file while encrypting it by cycling it k number of places where k is user-defined from 0-9. So far I've written a large amount of code but I think there's something wrong with it because it's not scanning anything.

    I'm supposed to encrypt the following:

    abcdef uvwxyz ZYXWVUTS RQPONML 102378

    by cycling it "k" number of places using the ASCII numbers

    Code:
    #include <stdio.h>
    
    
    
    FILE *f_in, *f_out;
    
    int var, k, status;
    char output, chrr;
    
    
    main()
            {
            printf("Enter a value from 0 to 9.\n");
            scanf("&#37;d", &k);
    
           		if (k>=0 && k<=9)
                            	{
                            	f_in = fopen("message.txt", "r");
                            	f_out = fopen("encrypted.txt", "w");
    
                            	//status = fscanf (f_in, "%c", &var);
    
                            	//chrr = (int) input
    
    
    
                            	while (var != EOF)
                                    		{
                                    		fscanf(f_in, "%d", &var);
                                    		fscanf(f_in, "%c", &chrr);
                                    		
    				if ((chrr>=48) && (chrr<=57))
                                            			{
                                            			if (chrr==57)
                                                    			{
                                                    			if(k==0)
    								{
                               				                        output=57;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=47+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
    							}
                            				
    						if (chrr==56)
                                                    			{
                                                    			if(k<=1)		
    								{
                                                            				output=56+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=46+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                       				}
    							}
    
                                           			 if (chrr==55)
                                                    			{
                                                    			if(k<=2)
    								{
                                                            				output=55+k;
                                                            				fprintf( f_out, "%c", output);
    					 			printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=45+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
                                            			if (chrr==54)
                                                    			{
                                                    			if(k<=3){
                                                            				output=54+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=44+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                           				}
                                                    			}
                                            			if (chrr==53)
                                                    			{
                                                    			if(k<=4){
                                                            				output=53+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                   		 	else    		{
                                                            				output=43+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
                                            			if (chrr==52)
                                                    			{
                                                   		 		if(k<=5){
                                                            				output=52+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=42+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
                                            			if (chrr==51)
                                                    			{
                                                    			if(k<=6){
                                                            				output=51+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=41+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
    						if (chrr==50)
                                                    			{
                                                    			if(k<=7){
                                                            				output=50+k;
                                                            				fprintf( f_out, "%c", output);
                                                           				printf("%c", output);
                                                            				}
                                                    			else    {
                                                            				output=40+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
                                            			if (chrr==49)
                                                    			{
                                                    			if(k<=8){
                                                            				output=49+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				}
                                                   		 		else    {
                                                            				output=39+k;
                                                            				fprintf( f_out,"%c", output);
                                                            				printf("%c",output);
                                                            				}
                                                    			}
    						if (chrr==48)
                                                    			{
                                                    			output=48+k;
                                                   				 fprintf( f_out, "%c", output);
                                                   				 printf("%c", output);
                                                				}
    						}
    
    
    
    
                                    		else if ((chrr>='a') && (chrr<='z'))
                                            			{
                                            			if (chrr=='z')
                                                    			{
                                                    			if(k==0){
                                                            				output=122;
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                           				}
                                                    			else    {
                                                            				output=96+k;
                                                            				fprintf( f_out,"%c", output);
                                                           				printf("%c",output);
                                                            				}
    
                                                   				 }
    						if (chrr=='y')
                                                   		 		{
                                                    			if(k==0 || k==1)
                                                            				{
                                                            				output=121+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=95+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			}
    
                                            			if (chrr=='x')
                                                    			{
                                                    			if(k>=0 && k<=2)
                                                           			 	{
                                                            				output=120+k;
                                                           				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=94+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                   				 }
                                            			if (chrr=='w')
                                                    			{
                                                   			 	if(k<=3)
                                                            				{
                                                            				output=119+k;
                                                           				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    	{
                                                            				output=93+k;
                                                            				fprintf( f_out, "%c", output);
                                                           				}
                                                     			}
    						if (chrr=='v')
                                                    			{
                                                    			if(k<=4)
                                                           			 	{
                                                            				output=118+k;
                                                            				fprintf( f_out, "%c", output);
                                                           				}
                                                    			else    {
                                                           				 output=92+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
    							}
                                                    				
    
                                          			if (chrr=='u')
                                                    			{
                                                    			if(k<=5)
                                                            				{
                                                            				output=117+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=91+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
    
                                            			if (chrr=='t')
                                                    			{
                                                    			if(k<=6)
                                                            				{
                                                            				output=116+k;
                                                           		 		fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=90+k;
                                                            				fprintf( f_out, "%c", output);
    								}
                                                     			}
                                            			if (chrr=='s')
                                                    			{
                                                    			if(k<=7)
                                                            				{
                                                           				output=115+k;
                                                            				fprintf( f_out, "%c", output);
                                                           			 	}
                                                    			else    {
                                                            				output=89+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
                                            			if (chrr=='r')
                                                    			{
                                                    			if(k<=8)
                                                           			 	{
                                                            				output=114+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=88+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			 }
    
                                            			else    {
    							while((chrr>='a') && (chrr<='q'))
                                                            				{
                                                            				output=(chrr+k);
                                                            				fprintf( f_out, "%c", output);
                                                            				printf("%c", output);
                                                            				break;
                                                            			}
                                                    		}
                                            	}
    
    
    
    
    
                            else if (chrr>='A' && chrr<='Z')
                                   		 {
                                           			 if (chrr=='Z')
                                                    			{
                                                    			if(k==0){
                                                            				output=90;
                                                            				fprintf( f_out, "%c", output);
    
                                                            				}
                                                    			else    {
                                                            				output=64+k;
                                                            				fprintf( f_out,"%c", output);
    
                                                            				}
    
                                                    			}
                                            			if (chrr=='Y')
                                                    			{
                                                    			if(k==0 || k==1)
                                                            				{
                                                            				output=89+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
    							else    {
                                                            				output=63+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			}
    
                                            			if (chrr=='X')
                                                   			 	{
                                                    			if(k>=0 && k<=2)
                                                            				{
                                                            				output=88+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=62+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			}
                                            			if (chrr=='W')
                                                    			{
                                                    			if(k<=3)
                                                            				{
                                                            				output=87+k;
                                                           		 		fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=61+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
    						if (chrr=='V')
                                                   			 	{
                                                    			if(k<=4)
                                                            				{
                                                            				output=86+k;
                                                           		 		fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=60+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
    
                                                    			}
    
                                            			if (chrr=='U')
                                                    			{
                                                   			 	if(k<=5)
                                                            				{
                                                            				output=85+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                           			 	output=59+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
    
                                            			if (chrr=='T')
                                                    			{
                                                    			if(k<=6)
                                                            				{
                                                            				output=84+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
    								output=58+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
                                            			if (chrr=='S')
                                                    			{
                                                    			if(k<=7)
                                                            				{
                                                            				output=83+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                    			else    {
                                                            				output=57+k;
                                                           			 	fprintf( f_out, "%c", output);
                                                            				}
                                                    			 }
                                            			if (chrr=='R')
                                                    			{
                                                   				 if(k<=8)
                                                           				{
                                                            				output=82+k;
                                                            				fprintf( f_out, "%c", output);
                                                           				 }
                                                    			else    {
                                                            				output=56+k;
                                                            				fprintf( f_out, "%c", output);
                                                            				}
                                                     			}
                                            			else if (chrr>='A' && chrr<='Q')
                                                    				{
                                                    				output=(chrr+k);
    								fprintf( f_out, "%c", output);
                                                    				printf("%c", output);
    
    
                                                    				}
    
                                           			 }
    
                                    		else
                                            		{
                                            		output=chrr;
                                            		fprintf( f_out, "%c", output);
                                            		printf("%c", output);
                                            		break;
                                            		}
                                    		}
                            fclose(f_in);
                            fclose(f_out);
    
    
    
                            }
    /*      else    {
                    printf("The integer is not from 0 to 9");
                    break;
                    }
    */
    }
    Now for some reason it's reading the scanf value but it doesn't write anything. It appears to go into the if/else loops but nothing happens.

    *EDIT* ok i put in the fscanf()'s inside the loop and changed their condition, now all my program is doing is printing d to the screen.
    *EDIT* It looks like it's encryping the first character in the loop then it's not stopping, perhaps it needs breaks?
    Last edited by 6kaine9; 10-19-2008 at 07:52 PM. Reason: made code more presentable

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well there's only one call to fscanf, and it isn't in your loop.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    6kaine9, here's some constructive criticism.

    Notice the pattens in your code:
    Code:
    #include <stdio.h>
    
    FILE *f_in, *f_out;
    
    int var, k, chrr, status;
    char output;
    
    main()
            {
            printf("Enter a value from 0 to 9.\n");
            scanf("&#37;d", &k);
    
                    if (k>=0 && k<=9)
                            {
                            f_in = fopen("message.txt", "r");
                            f_out = fopen("encrypted.txt", "w");
    
                            status = fscanf (f_in, "%c", &var);
    
                            //chrr = (int) input
    
    
    
                            while (status != EOF)
                                    {
                                    if ((chrr>=48) && (chrr<=57))
                                            {
                                            if (chrr==57)
                                                    {
                                                    output=k;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
                                            if (chrr==56)
                                                    {
                                                    output=k-1;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
    
                                            if (chrr==55)
                                                    {
                                                    output=k-2;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
    
                                            else
                                                    {
                                                    while((chrr>=48) && (chrr<=54))
                                                            {
                                                            output=(chrr+k);
                                                            fprintf( f_out, "%c", output);
                                                            printf("%c", output);
                                                            }
                                                    }
                                            }
    
    
    
    
                                    else if ((chrr>='a') && (chrr<='z'))
                                            {
                                            if (chrr=='z')
                                                    {
                                                    output=99;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
                                            if (chrr=='y')
                                                    {
                                                    output=98;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
    
                                            if (chrr=='x')
                                                    {
                                                    output=97;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
    
                                            else    {
                                                    while((chrr>='a') && (chrr<='w'))
                                                            {
                                                            output=(chrr+k);
                                                            fprintf( f_out, "%c", output);
                                                            printf("%c", output);
                                                            }
                                                    }
                                            }
    
    
    
    
    
                                    else if (chrr>='A' && chrr<='Z')
                                            {
                                            if (chrr=='Z')
                                                    {
                                                    output=67;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
                                            else if (chrr=='Y')
                                                    {
                                                    output=66;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
                                            else if (chrr=='X')
                                                    {
                                                    output=66;
                                                    fprintf( f_out, "%c", output);
                                                    printf("%c", output);
                                                    }
    
                                            else
                                                    {
                                                    while((chrr>='A') && (chrr<='Z'))
                                                            {
                                                            output=(chrr+k);
                                                            fprintf( f_out, "%c", output);
                                                            printf("%c", output);
                                                            }
    }
    
                                            }
    
                                    else
                                            {
                                            output=chrr;
                                            fprintf( f_out, "%c", output);
                                            printf("%c", output);
                                            }
                            fclose(f_in);
                            fclose(f_out);
                                    }
    
                            }
    /*      else    {
                    printf("The integer is not from 0 to 9");
                    }
    */
    }
    This is not what programming is all about. When you see patterns like these repeating themselves in your code, stop, then think about how you can structure your code to remove all this redundant code, and then fix it. This process I describe is called refactoring. It makes your programs easier to read and easier to debug.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    The printf function is just so I can see what it's writing to the file on the screen. As for the fprintf() I honestly don't know how to shorten that more than it already is.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    I would've loved to use just one loop for all the fprintf()'s but I honestly don't know how to make the ASCII numbers jump from like Z and go back up to A without specifically stating a condition for each one.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Check to see if this actually works:
    Code:
    f_in = fopen("message.txt", "r");
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Have you learned about the switch/case construct yet?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    I have used about the switch/case construct it's just that the assignment specifically calls for a user-defined value k to cycle the numbers, upper case letters, and lower case letters so I don't think the switch/case construct would work.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Better yet, have you learned about isupper, islower, and isdigit?

    Also note that if you shift over the line (i.e., past 'Z' or 'z' or '9') then going backwards by 26 (for the letters) or 10 (for the numbers) will get you back where you need to be.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    We have not read about the isupper, islower, and isdigit.

    That would be a different way of doing it sure but I'd still have to write the code.

    I can't exactly start from scratch this is due Tuesday at midnight and your suggestions, while great and constructive still doesn't answer my question as to why my code isn't working.
    Last edited by 6kaine9; 10-19-2008 at 05:03 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since you appear to close your input and output files after writing one character, what else do you expect? (I say appear because you I don't care enough to fix your indentation, but I think I counted the curly braces correctly.)

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    Thanks again to everyone for their help.

    Tabstop: You are correct the fclose is in the wrong spot, I moved it and it is now writing as it should, although there are some logical errors with my program still.

    I'm still having an issues with spaces, is the space not considered a character? My spaces are not transfering over. Do I need to have another special case for it?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That last else clause should take care of spaces. Granted, the code you have posted skips every other character, but hey.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    15
    Yeah the space isn't working. Maybe I have a stray bracket somewhere?

    What do you mean it skips every other character?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I'm looking at the code in the first post right now, 'cause I don't see anything more recent, and you read one character into var (which gets virtually thrown away, since we don't do anything with it) and one character into chrr.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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