-
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("%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?
-
Well there's only one call to fscanf, and it isn't in your loop.
-
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("%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.
-
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.
-
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.
-
Check to see if this actually works:
Code:
f_in = fopen("message.txt", "r");
-
Have you learned about the switch/case construct yet?
-
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.
-
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.
-
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.
-
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.)
-
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?
-
That last else clause should take care of spaces. Granted, the code you have posted skips every other character, but hey.
-
Yeah the space isn't working. Maybe I have a stray bracket somewhere?
What do you mean it skips every other character?
-
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.