Help needed with C++ code.

I have been working on some C++ code that doesn't seem to be going right.
I'm wanting it to read 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.
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.
Could anybody help me?

#include <iostream>
using namespace std;
int main()
{
int isolateDigits();
int replaceDigits();
int swapDigit1withDigit3();
int recompose();
int solutionOutput();
return(0);
}
{
int originalNumber, key

cout << "Enter the original three-digitnumber: ";
cin >> originalNumber;

cout << "Enter the key: ";
cin >> key;

cout << "The encrypted number for ", originalNumber, " is ", encryptedNumber, ".";
return(0);

}
void isolateDigits(int originalNumber)
{
const int Digit1, Digit2, Digit3;

Digit1 = originalNumber / 100;
Digit2 = (originalNumber % 100) / 10;
Digit3 = originalNumber % 10;
return(0);
}
void replaceDigits()
{
const int Digit1, Digit2, Digit3, key;

Digit1 = ((Digit1 + key) mod 10);
Digit2 = ((Digit2 + key) mod 10);
Digit3 = ((Digit3 + key) mod 10);
return(0);
}
void swapDigit1withDigit3()
{
const int Digit1, Digit3, temp;

temp = Digit1;
Digit1 = Digit3;
Digit3 = temp;
return(0);
}
void recomposeEncryptedNumber()
{
int encryptedNumber, Digit1, Digit2, Digit3;

encryptedNumber = (((Digit1 * 100) + (Digit2 * 10) + Digit3);
return(0);
}

I'm very new to this, where have I gone wrong?
First of all, please use code tags.
You're declaring functions inside of a function(main()), which is illegal.
You need to move the function declarations to global scope.

The input code is not bound to any function body. You've got loose braces.
Your function declaration for isolateDigits() states that the function doesn't take parameters, but in your definition you accept a parameter.
The return types on your function declarations don't match those of the definitions, and sometimes they are not necessary - and in some cases you are trying to return 0 from void functions, which is illegal.
The variables you use in your functions have been declared as const, which means they cannot be modified. You then attempt to modify them.
In addition, the variables in your functions are bound to the scope of the function, which means that the illegal operations you attempt to perform on them wouldn't matter anyways, because
the variables expire.

Take a look here. I've modified your code, and now it compiles and behaves as expected. I've elected to ignore implementing any error checking, such as if the number entered is more than three digits, or if std::cin ever raises any error flags.

If you have any questions regarding anything, feel free to ask.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

void isolateDigits(int number, int digits[]);
void replaceDigits(int key, int digits[]);
void swap(int& a, int& b);
int recompose(int digits[]);

int main() {
	int number, key;

	std::cout << "Enter three-digit number: ";
	std::cin >> number;

	std::cout << "Enter the key: ";
	std::cin >> key;

	int digits[3];
	isolateDigits(number, digits);
	replaceDigits(key, digits);
	swap(digits[0], digits[2]);

	std::cout << "Original number:\t" << number << std::endl;
	std::cout << "Ecnrypted number:\t" << recompose(digits) << std::endl;
	std::cin.sync();//This line in conjunction with the next one prevent the console from closing immediately, however, it's considered bad practice.
	std::cin.get();
	return 0;
}

void isolateDigits(int number, int digits[]) {
	digits[0] = number / 100;
	digits[1] = (number % 100) / 10;
	digits[2] = number % 10;
}

void replaceDigits(int key, int digits[]) {
	digits[0] = ((digits[0] + key) % 10);
	digits[1] = ((digits[1] + key) % 10);
	digits[2] = ((digits[2] + key) % 10);
}

void swap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

int recompose(int digits[]) {
	return ((digits[0]*100) + (digits[1] * 10) + digits[2]);
}
Last edited on
Topic archived. No new replies allowed.