Thread: Return 4 Values

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pass an array of 4 ints, then write your 4 values into that array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    How? Thats chapter 7 in our book and were only in chapter 4,is there another way???
    Last edited by freddyvorhees; 08-02-2008 at 09:11 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So, how does chapter 4 tell you to do it?

  5. #5

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    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.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Chapter 4 is about algorithm,pseudocode,loops,and if statements.Which one is useful?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you are meant to add them up into a single integer:

    Code:
    return encrypt1 + encrypt2 * 10 + encrypt3 * 100 + encrypt4 * 1000;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I tried that, but what if theres a zero?

  11. #11

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by anon View Post
    Perhaps you are meant to add them up into a single integer:
    Hmm, but that might remove leading 0's.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, but if the answer you get back is 17, you know the answer is therefore "0017". How is this hard?

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Ill just try this next week.Ill ask my professor if I could use arrays

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The leading zeros is nothing but output formatting.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    but what if the 0 is in the tenths place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM