Returning multiple values from function??

This is a discussion on Returning multiple values from function?? within the C++ Programming forums, part of the General Programming Boards category; i am supposed to write a program to display the status of an order for a company. "The program should ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    Returning multiple values from function??

    i am supposed to write a program to display the status of an order for a company.

    "The program should have a function that asks for the following data:
    - The number of spools ordered.
    - The number of spools in stock.
    - If there are special shipping and handling charges.

    The gathered data should be passed as arguments to another function that displays blablabla"

    my question is how does the first function return all three pieces of information to main so it can be passed to the second function?
    i just made three separate functions for it and it works but it seemed like the book implied one function should do all that work.
    fyi structs and classes have not been covered yet so please dont reply with an answer in that form of solution.

    here is my code: (not perfected yet with calculations but just want answer to my question)

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void getOrdered(double &);
    void getStock(double &);
    double specialShipping();
    void displayData(double, double, double);
    
    int main()
    {
    	double ordered, stock;
    	double shipping;
    
    	getOrdered(ordered);
    	getStock(stock);
    	shipping = specialShipping();
    	displayData(ordered, stock, shipping);
    
    	return 0;
    }
    
    void getOrdered(double &ord)
    {
    	cout << "Enter the number of spools ordered: ";
    	cin >> ord;
    }
    
    void getStock(double &st)
    {
    	cout << "Enter the number of spools in stock: ";
    	cin >> st;
    }
    
    double specialShipping()
    {
    	char choice;
    	double shipping = 0;
    
    	do
    	{
    	cout << "Are there any special shipping options? (Y or N): ";
    	cin >> choice;
    	}while(choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N');
    
    	if (choice == 'y' || choice == 'Y')
    	{
    		cout << "Enter the shipping in dollars per spool: ";
    		cin >> shipping;
    	}
    	else if (choice == 'n' || choice == 'N')
    	{
    		shipping = 10;
    	}
    
    	
    	return shipping;
    }
    
    void displayData(double ord, double st, double ship)
    {
    	if (ord > st)
    	{
    		cout << "Number of spools on backorder: " << ord - st << endl;
    	}
    	else
    		cout << "Number of spools ready to ship: " << ord << endl;
    	
    	cout << "Subtotal of portion ready to ship: $" << fixed << setprecision(2) << (ord * 100) << endl;
    	cout << "Total shipping and handling: $" << fixed << setprecision(2) << ship << endl;
    	cout << "Total: $" << fixed << setprecision(2) << (ord*100) + ship << endl;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You can pass all the values by reference.
    Code:
    void get_all_3(int& spools_ordered, int& spools_in_stock, bool& special_charges)
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Or you could put all three values into a struct or class and return an instance of that class by value:
    Code:
    struct data
    {
        data(int spools_ordered, int spools_in_stock, bool special_charges) :
            spools_ordered(spools_ordered),
            spools_in_stock(spools_in_stock),
            special_charges(special_charges)
        {
        }
        int spools_ordered;
        int spools_in_stock;
        bool special_charges;
    };
    (or there's aggregate initialisation I guess, which would make it shorter)

    Then in your function:
    Code:
    return data(ordered, in_stock, special);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    The question expects you to use references as per bithub's example (since you aren't using structs/classes, let alone boost).

    Just to be thorough, in Python multiple return values are built in as tuples. There's boost::tuple.

    Code:
    return boost::tuple<double, double, double>(1.0, 2.0, 3.0);
    Technically, if you wanted to use the same function, and needed to fill out different variables, you could use an argument to define the type you want returned (ie. 1 equals ordered, 2 equals stock, etc.) and just use an if statement or switch. That's bad and I've never done it, but still works in the context described.

    Code:
    double get_each(int type)
    {
      if(type == 1)
      {
        return spools_ordered;
      }
      else if(type == 2)
      {
        return spools_in_stock;
      }
      else if(type == 2)
      {
        return special_charges;
      }
      else
      {
        //error
        return -1.0;
      }
    }
    Last edited by Dae; 08-18-2009 at 03:49 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 09:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21