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]);
}
|