Thread: starters question about handling data types.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    starters question about handling data types.

    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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Arithmetic -- dividing by ten, or hundred, or thousand; taking remainders when dividing by ten, or hundred, or thousand....

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    Oh okay, thanks lol... should of figured that out myself I think

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Hint:

    4789 / 1000 = 4
    4789 &#37; 1000 = 789

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    ok thnx, I got it now...

    can I modify my program to run better? or is this pretty much what was expected from me?
    Code:
    #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;
    }
    I am going to build the decryption application tomorrow, for I am tired

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> (value&#37;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?).

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    Quote Originally Posted by Daved View Post


    Your encryption function is not quite right. You are adding the digit to itself before adding seven to it
    isnt that supposed to be?
    woops sorry, I did not interpret this the right way...
    "Replace each digit by (the sum of that digit plus 7)"

    following an english book when you have not mastered English itself is pretty hard, tnx for helping me!

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2008, 01:54 PM
  2. Handling Large Amounts of Data
    By kas2002 in forum C++ Programming
    Replies: 9
    Last Post: 12-21-2006, 04:52 AM
  3. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  5. Data types in Unicode
    By Garfield in forum Windows Programming
    Replies: 12
    Last Post: 10-28-2001, 10:48 AM