yeliah
yeliah

Reputation: 41

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;

Upvotes: 0

Views: 347

Answers (3)

Indranil
Indranil

Reputation: 136

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)

Upvotes: 0

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

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()

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 595319

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...
    ...
}

Upvotes: 0

Related Questions