I am following this book and it wants me to encrypt a 4-digit integer that the user inputs.
so the user Inputs 4789 for example
now I want to perform actions on each of the digits separately. How can I work with the
4 / 7 / 8 / 9 separately?
I am following this book and it wants me to encrypt a 4-digit integer that the user inputs.
so the user Inputs 4789 for example
now I want to perform actions on each of the digits separately. How can I work with the
4 / 7 / 8 / 9 separately?
Arithmetic -- dividing by ten, or hundred, or thousand; taking remainders when dividing by ten, or hundred, or thousand....
Oh okay, thanks lol... should of figured that out myself I think
So what stage of the book are you at (what is the name of the chapter, what sort of things have you been tought in that chapter)?
I can think of at least three very different methods to do that, but which one to suggest depends on whether you are at one of the first three chapters or further on in the chapter.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Uhm Im learning about control statements now....
If else while etc... although I already know about these because I also program some PHP... but PHP does not require to declare variable types...
So what variable types do you think you need (and which ones have you been told about so far)?
Also, I take it that you have an encryption where you "rotate" a number 3 steps like on one of those bicycle/briefcase locks, so 1 rotated 2 steps makes 3, 9 rotated 3 steps makes 2, etc, yeah?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
uhm the asignement is like this :
/*
(Cryptography) A company wants to transmit data over the telephone,
but is concerned that its phones could be tapped.
All of the data are transmitted as four-digit integers.
The company has asked you to write a program that encrypts
the data so that it can be transmitted more securely.
Your program should read a four-digit integer and encrypt it as follows:
Replace each digit by (the sum of that digit plus 7) modulus 10.
Then, swap the first digit with the third,
swap the second digit with the fourth and print the encrypted integer.
Write a separate program that inputs an encrypted
fourdigit integer and decrypts it to form the original number.
*/
I know about INT and string... but just to use, not advanced...
btw i still did not find a solution...
So start with a program that can read in an integer and print it.
Then add functionality to print each digit out of the number using the method of dividing and using modulo to find the remainder.
Then add the functionality to modify each digit, and then put it back together into a single integer again.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I dont quite understand how to use the modulo...
How should I work it to get each digit?
first digit, divide by 1000 but how do I get the second,third and forth one?
this is what I got till now:
Code:int Encrypt (int value) { // do it } int main () { int to_be_encrypted,encrypted; std::cout <<"Please enter a 4 digit integer number for encryption"; std::cin >> to_be_encrypted; encrypted = Encrypt(to_be_encrypted); std::cout <<"The original number: " << to_be_encrypted << std::endl; std::cout <<"The encrypted number: " << encrypted << std::endl; std::cin.get(); return 0; }
Last edited by epidemic; 07-21-2008 at 02:06 PM.
Hint:
4789 / 1000 = 4
4789 % 1000 = 789
ok thnx, I got it now...
can I modify my program to run better? or is this pretty much what was expected from me?
I am going to build the decryption application tomorrow, for I am tiredCode:#include <iostream> int Encrypt (int value) { // compute seperate digits. int First,Second,Third,Forth; First = value/1000; Second = (value%1000) / 100; Third = (value%1000) % 100 / 10; Forth = value % 10; First = (First+First+7) % 10; Second = (Second+Second+7) % 10; Third = (Third+Third+7) % 10; Forth = (Forth+Forth+7) % 10; int Encrypted_number = (Third*1000)+(Forth*100)+(First*10)+(Forth); return (Encrypted_number); } int main () { int to_be_encrypted,encrypted; std::cout <<"Please enter a 4 digit integer number for encryption"; std::cin >> to_be_encrypted; encrypted = Encrypt(to_be_encrypted); std::cout <<"The original number: " << to_be_encrypted << std::endl; std::cout <<"The encrypted number: " << encrypted << std::endl; std::cin.get(); std::cin.get(); return 0; }
>> (value%1000) % 100
That's the same as (value % 100).
Your encryption function is not quite right. You are adding the digit to itself before adding seven to it, but that messes up the encryption. There's also a bug when you recombine the encrypted digits (where's second?).
And it's actually a good idea to spell things right:
Fourth should probably be the name of that variable.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Then there's the easiest encryption:
Code:#include <stdio.h> char EncryptChar( char c ) { return ~c; } void EncryptStr( const char* in, char* out ) { while ( *in ) { *out = EncryptChar( *in ); ++in; ++out; } *out = '\0'; } int main() { const char str[] = "Hello World"; char encryptStr[30]; EncryptStr( str, encryptStr ); printf( "Original: (%s)\nEncrypted: (%s)\n", str, encryptStr ); return 0; }