Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,510 people online right now. Registration is fast and FREE... Join Now!




Encryption/Decryption Problem

 
Reply to this topicStart new topic

Encryption/Decryption Problem, Homework help needed

PatCPA
13 Oct, 2006 - 01:29 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 5


My Contributions
Hello All! I need some help with this encryption/decryption problem. I am to promt user to to either 'e' encrypt or 'd' decrypt a four digit number. Each digit will be replaced by the sum of the digit plus 7 (modulus 10). Here's what I have so far.

CODE
#include<iostream>
#include<string>
#include<cmath>

using namespace std;

int main()
{
char action; // Prompt user to decide action

int encrypt;
int decrypt;

cout << "Enter 'e' to encrypt or 'd' to decrypt" << endl;
cin >> action; // Get user input for action

if (action == 'e')
{
cout << "Enter a 4-digit number to encrypt: " << endl;
cin >> encrypt;
cout << encrypt << " would be encrypted as " << char1 << char2 << char3 << char4 << endl;
}
else
if (action == 'd')
{
cout << "Enter a 4-digit number to decrypt: " << endl;
cin >> decrypt;
}
else
cout << "INVALID ENTRY! Please enter 'e' to encrypt or 'd to decrypt" << endl;


return 0;
}


This post has been edited by Dark_Nexus: 31 Oct, 2006 - 10:11 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Encryption/Decryption Problem
13 Oct, 2006 - 02:41 PM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(PatCPA @ 13 Oct, 2006 - 02:29 PM) *

Hello All! I need some help with this encryption/decryption problem. I am to promt user to to either 'e' encrypt or 'd' decrypt a four digit number. Each digit will be replaced by the sum of the digit plus 7 (modulus 10). Here's what I have so far.

CODE

int encrypt;
int decrypt;

if (action == 'e')


Pat,

You are using an int as the type then expecting it to match a string... change the type to a char,,,,
User is offlineProfile CardPM
+Quote Post

PatCPA
RE: Encryption/Decryption Problem
13 Oct, 2006 - 02:49 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 5


My Contributions
It can process the selection for encryption or decryption. The problem I am having is that when entering a 4-digit number, how do i get it to extract each digit do that I can manipulate it for encryption or decryption?
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Encryption/Decryption Problem
13 Oct, 2006 - 02:56 PM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
so, thats the problem

first, you need to convert your integer into a string... then you can access each digit....

How? lookup the conversion function itoa( ... )
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Encryption/Decryption Problem
13 Oct, 2006 - 03:06 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
As a note for portability, itoa() is not defined in ANSI C...it will not be supported by all compilers. It may be easier to take the input as a string, and convert to int using the atol(), which is defined in ANSI C. Well, not easier, but more portable in any case.
User is offlineProfile CardPM
+Quote Post

bluesuus
RE: Encryption/Decryption Problem
13 Oct, 2006 - 04:56 PM
Post #6

D.I.C Head
Group Icon

Joined: 26 Dec, 2005
Posts: 57


Dream Kudos: 25
My Contributions
Yo man all u need to do is apply successive division and modulo.e.g lets say u hv 1234 as ur 4digit number.if you take the modulo of it by 10, you will get the last integer which is 4 in this case( 1234%10 ).And also when you divide the number by 10 you will get 123.4( 1234/10 ), so u save the result in an integer so it will be 123 since u have already taken the last digit and stored in a variable. take ur new number and modulo 10 again u will get 3......and so on...
User is offlineProfile CardPM
+Quote Post

PatCPA
RE: Encryption/Decryption Problem
14 Oct, 2006 - 03:03 PM
Post #7

New D.I.C Head
*

Joined: 13 Oct, 2006
Posts: 5


My Contributions
I need to extract each of the 4 digits entered in order to perform the required operation. Show I change the variables (encrypt/decrypt) to INT or leave it as a strinf? Don't I have to have the variables be INT in order to perform the manipulations?
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Encryption/Decryption Problem
14 Oct, 2006 - 03:28 PM
Post #8

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(PatCPA @ 14 Oct, 2006 - 04:03 PM) *

I need to extract each of the 4 digits entered in order to perform the required operation. Show I change the variables (encrypt/decrypt) to INT or leave it as a strinf? Don't I have to have the variables be INT in order to perform the manipulations?

C/C++ can automatically perform the data type change (since its all binary data anyway), however, bluesuus has offered you the best solution as it savesyou changing the data type.

Based on the initial data type, and the algorithm, it looks like you should perform this using int's.

The algorithm can detect when you have encrypted all digits because the integer division will leave you with a value of zero.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:12PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month