# Return 4 Values

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-02-2008
freddyvorhees
Return 4 Values
How can I return 4 values???

Code:

```#include <iostream> using namespace std; double encrypt(int number) {         int digit1, digit2, digit3, digit4, encrypt1, encrypt2, encrypt3, encrypt4;                 digit4 = number % 10;         digit3 = ((number % 100) - digit4)/10;         digit2 = ((number % 1000) - digit3)/100;         digit1 = ((number - number % 1000))/1000;                 encrypt1 = (digit3 + 7)%10;                encrypt2 = (digit4 + 7)%10;         encrypt3 = (digit1 + 7)%10;         encrypt4 = (digit2 + 7)%10; } int main() {         int num; cin>>num; cout<<encrypt(num); }```
• 08-02-2008
Salem
Pass an array of 4 ints, then write your 4 values into that array.
• 08-02-2008
freddyvorhees
How? Thats chapter 7 in our book and were only in chapter 4,is there another way???
• 08-02-2008
rags_to_riches
So, how does chapter 4 tell you to do it?
• 08-02-2008
rudyman
You could return an array and have an (inline) function to print out that array. It may look obscure, unless you want to use dynamic memory:

Code:

`// Edited out the solution.`
• 08-02-2008
freddyvorhees
Chapter 4 is about algorithm,pseudocode,loops,and if statements.Which one is useful?
• 08-02-2008
Elysia
Erm, none of them really covers returning of multiple values.
There are 3 ways, that I can see:
- Create a structure to hold the data.
- Create an array to hold the data.
- Pass 4 variables directly.

And they can be used in 2 ways:
- Pass a structure/array by reference to the function.
- Create a temporary structure and return it.
- Create an array using dynamic memory (new/std::vector/std::tr1::array/std::tr1::shared_ptr) and return it.

The last can be used in simply one way: by passing 4 variables by reference to hold the data you want to return.
Pick a method you know.
• 08-02-2008
freddyvorhees
Honestly I dont know any of that.Maybe I,m doing something wrong.

Heres what im supposed to do:

(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 four digit integer and decrypts it to
form the original number.

--------------------------------------------------
Sample Run of the encrypting program
--------------------------------------------------

Enter numbers to encrypt (0000 to end):

1234 1111 3290 1800 0000

The encrypted numbers are:

0189 8888 6709 7785

--------------------------------------------------
Sample Run of the decrypting program
--------------------------------------------------

Enter numbers to decrypt (0000 to end):

0189 8888 6709 7785 0000

The decrypted numbers are:

1234 1111 3290 1800
• 08-02-2008
anon
Perhaps you are meant to add them up into a single integer:

Code:

`return encrypt1 + encrypt2 * 10 + encrypt3 * 100 + encrypt4 * 1000;`
• 08-02-2008
freddyvorhees
I tried that, but what if theres a zero?
• 08-02-2008
rudyman
Quote:

Originally Posted by anon
Perhaps you are meant to add them up into a single integer:

Hmm, but that might remove leading 0's.
• 08-02-2008
tabstop
Yes, but if the answer you get back is 17, you know the answer is therefore "0017". How is this hard?
• 08-02-2008
freddyvorhees
Ill just try this next week.Ill ask my professor if I could use arrays
• 08-02-2008
anon
The leading zeros is nothing but output formatting.
• 08-02-2008
freddyvorhees
but what if the 0 is in the tenths place
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last