encripting 4-digit integer?

This is a discussion on encripting 4-digit integer? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<conio.h> using std::cout; using std::cin; using std::endl; int main() { // declare variables int num,digit1,digit2,digit3,digit4,sum1,sum2,sum3,sum4; // initialize variables ...

  1. #1
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Post encripting 4-digit integer?

    Code:
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        // declare variables
        
        int num,digit1,digit2,digit3,digit4,sum1,sum2,sum3,sum4;
        
        // initialize variables
        
        sum1=0,sum2=0,sum3=0,sum4=0;
        
        cout<<"\nEnter a 4 digit integer:\t";
        cin>>num;
        
        // separate the integers
        
        digit1=num%10;
        
        num/=10;
        digit2=num%10;
        
        num/=10;
        digit3=num%10;
        
        num/=10;
        digit4=num%10;
        
        // replace digits
        
        sum1=(digit1+7)%10;
        sum2=(digit2+7)%10;
        sum3=(digit3+7)%10;
        sum4=(digit4+7)%10; 
    
        // swap digits
        
        cout<<"Encripted integer\t\t";
        
        cout<<sum2<<sum1<<sum4<<sum3;
        
    getch();
    }
    Is this correct encription?I was trying to swap the first digit with the third one and second with the fourth one, ofcourse after replacing each digit with its sum plus 7 % 10.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    As an important side note, I suggest reading up on arrays and vectors so that you don't have to declare four similar, sequential variables like that.

    As for your encryption scheme, it appears to me that it would work. By adding seven, you've just got a simple caesar shift there. Then you mix up the digits.

    I think the real test will now be if you can decrypt those messages. Swap the digits back correctly, and shift back to the correct position. That ought to do it. Good luck.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Exclamation decripting 4-digit integer/

    Code:
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        // declare variables
        
        int num,digit1,digit2,digit3,digit4,dcript1,dcript2,dcript3,dcript4;
        
        // initialize variables
        
        dcript1=0,dcript2=0,dcript3=0,dcript4=0;
        
        // read integer
        
        cout<<"\nEnter the integer to be decripted:\t";
        cin>>num;
        
        // separate integers
        
        digit1=num%10;
        num/=10;
        digit2=num%10;
        num/=10;
        digit3=num%10;
        num/=10;
        digit4=num%10;
        num/=10;
        
        // print decripted digit
        
        if((digit1<7||digit2<7)||(digit3<7||digit4<7))
        {
            dcript1=(10+digit1)-7;
            dcript2=(10+digit2)-7;
            dcript3=(10+digit3)-7;
            dcript4=(10+digit4)-7;
            
            cout<<"\nDecripted 4-digit integer:\t\t";
            cout<<dcript2<<dcript1<<dcript4<<dcript3;
        }
        else
            dcript1=(digit1-7)%10;
            dcript2=(digit2-7)%10;
            dcript3=(digit3-7)%10;
            dcript4=(digit4-7)%10;
               
    getch();
    }
    See the attachment , the only prob is it outputs last integer thrice.

  4. #4
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Thumbs down sorryculdn't attach the file

    sorry couldn't attach the file

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Smile

    Honestly dude XMAS?? There no rest for the wicked??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    else
            dcript1=(digit1-7)%10;
            dcript2=(digit2-7)%10;
            dcript3=(digit3-7)%10;
            dcript4=(digit4-7)%10;
    Did you mean to have a pair of braces around that? Otherwise the last three always happen.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 06:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21