Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>		//for cin >> and cout <<
using namespace std;

int main()			// main algorithm
{
    int n1, n2, n3, key, originalnumber, encryptednumber;
    
    cout << ("Enter original number ");
    cin >> (originalnumber);
    cout << ("enter encryption number ");
    cin >> (key);
    
    void isolatedigits();
    n1 = originalnumber / 100;
    n2 = (originalnumber % 100) / 100;
    n3 = originalnumber % 10;
    
    void replacedigits();
    n1 = n1 + key;
    n2 = n2 + key;
    n3 = n3 + key;
    
    void swapdigits();
    n1 = n3;
    n3 = n1;    

    void recomposeencryptednumber();
    n1 = n1 * 100;
    n2 = n2 * 10;
    encryptednumber = n1 + n2 + n3;
    
    isolatedigits();
    replacedigits();
    swapdigits();
    recomposeencryptednumber();
    cout << (encryptednumber);
    
    system("pause");		//to hold the output screen
    return(0);
}
Posted
Updated 4-Nov-14 9:25am
v2
Comments
joshrduncan2012 4-Nov-14 15:12pm    
You aren't including anything in the #include statement. That's why your code doesn't work.
PIEBALDconsult 4-Nov-14 16:42pm    
No, that's just the XML encoding process.

You have a number of redundant function definitions inside main, none of which have any bodies, so they don't do anything. They should all be removed, leaving something like:
C++
#include <iostream>		//for cin >> and cout <<
using namespace std;
 
int main()			// main algorithm
{
    int n1, n2, n3, key, originalnumber, encryptednumber;
    
    cout << ("Enter original number ");
    cin >> (originalnumber);
    cout << ("enter encryption number ");
    cin >> (key);
    
    n1 = originalnumber / 100;
    n2 = (originalnumber % 100) / 100;
    n3 = originalnumber % 10;
    
    n1 = n1 + key;
    n2 = n2 + key;
    n3 = n3 + key;
    
    n1 = n3;
    n3 = n1;    
 
    n1 = n1 * 100;
    n2 = n2 * 10;
    encryptednumber = n1 + n2 + n3;
    
    cout << (encryptednumber);
    
    system("pause");		//to hold the output screen
    return(0);
}
</iostream>
 
Share this answer
 
What Richard said in Solution 1 is correct. I assume, you have "found" the code somewhere and the original author was in the process of refactoring the code into several independent functions. You caught it right in the middle of things :-)

Regarding the algorithm, there are several things that can't work:
C++
n1 = originalnumber / 100;
n2 = (originalnumber % 100) / 100;
n3 = originalnumber % 10;

Obviously, n1 to n3 should receive the digits of a three-digit decimal number. In that case the second statement needs to be:
C++
n2 = (originalnumber % 100) / 10;

In the next section you want to modify each of the digits by the key. But for the following code to work, this modification must remain in the range of 0 ... 9. Hence that section should read:
C++
n1 = (n1 + key) % 10;
n2 = (n2 + key) % 10;
n3 = (n3 + key) % 10;

And lastly, exchanging the values of two variables cannot be done by:
C++
n1 = n3;
n3 = n1;

All that does is to assign to value of n3 to both, n3 and n1. It should probably read:
C++
int temp = n1;
n1 = n3;
n3 = temp;
 
Share this answer
 
Thanks, this was my code from scratch i have a specification to create a program from which is

Quote:
1. Specification
. Look at the following partial PDL program that reads a (three-digit) integer representing the value to be encrypted, a (one-digit) integer representing the encryption key, encrypt the value and print the encrypted value. The encrypting method used is that each digit in the given number is replaced by ((the sum of that digit plus key) modulo 10) then the first and last “encrypted” digits are swapped.
produceEncryptedNumber
output( "Enter the original three-digit number: ")
input( originalNumber) //read in a (three-digit) number
output( "Enter the key: ")
input( key) //read in a (one-digit) number
call isolateDigits //find out the 3 digits that make up the number
call replaceDigits //’encrypt’ each of the three digits
call swapDigit1WithDigit3 //swap first and last digit
call recomposeEncryptedNumber //recreate encrypted number from ‘encrypted’ values
//output encrypted number
output( "The encrypted number for ", originalNumber, " is ", encryptedNumber, ".")
For example, if the number entered was 216 and the key given was 7, after applying the encryption procedure described the first digit (2) would become 9, the middle digit (1) would become 8 and the last digit (6) would become 3. The first and last encrypted digits are then swapped. The program displays the encrypted number: that is 389 in this case.
 
Share this answer
 
Comments
Richard MacCutchan 5-Nov-14 4:39am    
This is not a solution, and we have given you some hints, so you now need to look at them and your code and try and correct it. For example if you want to call a function (e.g isolateDigits) from main then the code for that function needs to be outside of main, and will probably need some input parameters and to return some value. See http://msdn.microsoft.com/en-us/library/9w92dxk3.aspx for more details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900