简体   繁体   中英

How do I return multiple values in a function C++?

I am writing some code and ran into an error when I need to return multiple values to main() from another function.

Here, I am trying to return item and total to the main() function. However, I am getting a warning saying that item has not been used, but I am using it in main() , where it then says "use of undeclared identifier" along with total .

Could someone help me with my syntax issue here?

int processSelection() {
  cout << "Enter your selection: " << flush;
  int item;
  cin >> item;

  cout << menuItems[item-1] << ": $" << cost[item-1] << " has been added to cart." << endl;

  int total;
  total = 0;
  total = total + cost[item];
  
  return (item, total);
}

int main() {
  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;

  vendingMachine();
  cout << "Enter 0 to checkout" << endl;

  int selection(item) = processSelection();

  float cost;
  
  while(selection != 0) {

    processSelection();
  } 
  cout << "Proceding to checkout..." << endl;
  cout << "========================" << endl;
  
  cout << "Amount due: " << total << endl;

Edited code: (I still get an error for return std::make_pair(item, total); and p = processSelection(); )

int processSelection() {
  cout << "Enter your selection: " << flush;
  int item;
  cin >> item;

  cout << menuItems[item-1] << ": $" << cost[item-1] << " has been added to cart." << endl;

  int total;
  total = 0;
  total = total + cost[item];
  
  return std::make_pair(item, total);

}

int main() {
  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;

  vendingMachine();
  cout << "Enter 0 to checkout" << endl;

//  int selection() = processSelection();
  std::pair<int, int> p = processSelection();

  float cost;
  
  while(p.first != 0) {

    processSelection();
  } 
  cout << "Proceding to checkout..." << endl;
  cout << "========================" << endl;
  
  cout << "Amount due: " << p.second << endl;

Use the below in your processSelection referencing https://www.geeksforgeeks.org/returning-multiple-values-from-a-function-using-tuple-and-pair-in-c/ from Mike.

return std::make_pair(item, total);

and call using in your main.

std::pair<int, int> p = processSelection();

Then you can use p.first and p.second to access the values.

As well as change int processSelection() to std::pair<int, int> processSelection()

Your function is declared to return a single int , but then you try to return multiple int values. That will simply not work.

You need to change the function to return a type that can actually hold multiple values at a time, eg:

struct ItemTotal {
    int item;
    int total;
};

ItemTotal processSelection() {
    ItemTotal it;
    ...
    it.item = ...;
    it.total = ...;
    ...
    return it;
}

int main() {
    ...
    ItemTotal selection = processSelection();
    // use selection.item and selection.total as needed...
    ...
}

You could just declare 'item' and 'total' in the main function and pass their reference to the processSelection() function like so:

void processSelection(int &item,int &total)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM