• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Simple C encrypt/decrypt program

Status
Not open for further replies.

locutus24

[H]ard|Gawd
Joined
Sep 13, 2004
Messages
1,642
Having some issues with a basic program I am messing around with.

The intent of this program is just that it uses a multiplier of 2 as a key for shifting a number that a user enters, eventually it will accept character input, followed by whole files. First off I am just learning how to use function prototypes and branching/looping within my programs.

My problem is simple, but I can't seem to solve it. When the first iteration of my loop completes, it wouldn't ask for additional user input, so then I inserted a second print function with my defined string. What I wanted would then print out, but instead of one time, it printed out twice. I figure it has something to do with my while loop or that I just put things out of order.

Sample:
"Here it is encrypted: 10, and decrypted: 20.00
Enter 'E' to encrypt, 'D' to decrypt
Enter 'E' to encrypt, 'D' to decrypt"

Code:
/*Preprocessor area, has strings that I don't want to
individually type in, and it also my two functions
which break out the work*/

#include <stdio.h>
#define CHOICE "Enter 'E' to encrypt, 'D' to decrypt"
#define EN "Enter integer for encrypt"
#define DE "Enter integer for decrypt"
float encryp1 (int);
float decryp1 (int);

int main(void)
{
	int a;
	char b;
	
	printf ("%s\n", CHOICE);	//This is the first instance
	
	while (scanf ("%c", &b) == 'E' | 'D' | 1){
	if (b == 'E'){
		printf ("%s\n", EN);
		scanf ("%d", &a);
		printf ("Here it is before: %d, and after: %.2f\n",
			a, encryp1(a));
		}
	else if (b == 'D'){
		printf ("%s\n", DE);
		scanf ("%d", &a);
		printf ("Here it is encrypted: %d, and decrypted: %.2f\n",
			a, decryp1(a));
		}
	printf ("%s\n", CHOICE); //This won't show-up unless I type it
				//here, but then it prints twice
	}
	
	return 0;
}

//Encryption fuction
float encryp1 (int a)
{
	int b;
	b = a / 2;
	return b;
}

//Decryption function
float decryp1 (int a)
{
	int b;
	b = a * 2;
	return b;
}

If someone could provide feedback as to why it is double printing, would greatly appreciate that. :)
 
Is this in the context of a class (if not, please elaborate on the context)? There are many things wrong/out of place here, that aren't related to your immediate question; but I don't want to interrupt the learning process.

This would be a great time to use a debugger. With a debugger, you can run each line of code, one at a time, and see what the values of all your variables are. Sometimes, we don't have access to a debugger, so we have to use the "printf school of debugging"... I would suggest adding a statement in your loop that prints out the important variables so you can see what's going on.
 
wow, I have no idea how that scanf and that printf stuff works, lol

Anyways, try this

Code:
if ( b == '\n') 
  	   printf ("%s\n", CHOICE);

so that it doesn't print while going through the while loop twice after the enter event (which since I don't know how scanf works, I don't know why)
 
Is this in the context of a class (if not, please elaborate on the context)? There are many things wrong/out of place here, that aren't related to your immediate question; but I don't want to interrupt the learning process.

This would be a great time to use a debugger.

Not academic related, just tried C++ a few years back but was quickly overwhelmed, so I have had a bit more time on my hands recently and have taken it as an opportunity for learning C since most sources said that it was a small and flexible language.

Going to google it out, but any suggestions on a good debugger for Linux, currently running Fedora, using Gedit and GCC for all my programming needs.
 

Tried break, but it killed the program altogether. Going to look into using goto, because I half-ass solved it by moving my printf statement, now though it prints when it goes into the other function.

Thanks for the assist, still any input on a good debugger would be appreciated:)
 
Last edited:
The two problems I see stem from scanf().

You can use gdb as a debugger.
Compile with -ggdb (or -g). Start gdb with your executable and set a breakpoint and you can examine your variables.

Code:
$ gcc -g -ggdb test.c -o test

Google for a gdb tutorial :)
Quick and dirty, I did something like the below to set a break point on line 20, start the program, and then print b. You can also "step" through lines of code.

Code:
$ gdb test
(gdb) break 20
...
(gdb) start
...
(gdb) print b
...
 
Don't #define your strings like that

You can do something like

char choice[] = "Enter 'D' to decrypt, 'E' to encrypt";

Your encrypt/decrypt function prototypes state that they will return a 'float' bur you are returning the variable 'b' which is an 'int'. That also creates the wrong answer if you call encrypt with a parameter that's an odd number. You get 2.00 instead of 2.5 for 5, for example.

And as you started to find out, your loop is printing twice because it's structured wrong. I would try something like creating a loop that runs forever and then breaking out of it based upon input. Maybe the user could enter 'Q' to quit. Then put the prompt inside the loop.
 
Ok, since this isn't academic; I'll be a bit more direct..

do a printf("Char from scanf %d\n", b); as the first line in your loop (this will be the ascii value).

Your loop condition is also all kinds of messed up :) | is bitwise or, || is logical or, and stuff == 'A' || 'B' || 'C' is not going to do what I think you meant.
 
i know this was just a trial run to get it to work, but using a,b,c, ect as variable names is a bad idea. use something a bit more descriptive, it makes the program easier to read and spot problems.

I would also mirror above where it was said to encompass the whole thing in a loop with q to quit. just make sure you have a function ( or line) that converts the input to uppercase(or lowercase) to do the comparing.
 
It's a strange coincidence then that the week two pset (or week 3?) of CS50x involves encryption and decryption. And the CS50 appliance runs Fedora and uses gcc and gedit. Not sure how common that setup is in reality. Just saying... :)
 
It's a strange coincidence then that the week two pset (or week 3?) of CS50x involves encryption and decryption. And the CS50 appliance runs Fedora and uses gcc and gedit. Not sure how common that setup is in reality. Just saying... :)
LOL

Sadly this person will graduate with a degree and get a very well paying job at some sucker of a company
 
Status
Not open for further replies.
Back
Top