0

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;
yeliah
  • 41
  • 6
  • check this article: https://www.geeksforgeeks.org/returning-multiple-values-from-a-function-using-tuple-and-pair-in-c/ – Mike67 Aug 05 '20 at 01:16
  • Looks like you're wanting to return `std::pair` – user4581301 Aug 05 '20 at 01:16
  • Marginally related, but worth your time to read so you know what the comma operator does in C++: https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator – user4581301 Aug 05 '20 at 01:18
  • 2
    Use pairs, tuples, structures or clases. Or (less recommended) non-const reference arguments. – Some programmer dude Aug 05 '20 at 01:22
  • 2
    My suggestion is using a `struct` which provides more meaningful info about what first and second values are rather than just "first and second". – Louis Go Aug 05 '20 at 01:23
  • how do i use pairs here? – yeliah Aug 05 '20 at 01:27
  • Does this answer your question? [Returning multiple values from a C++ function](https://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function) – JaMiT Aug 05 '20 at 02:14

3 Answers3

2

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

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • An other choice to handle return is using `std::tie`. – yao99 Aug 05 '20 at 01:44
  • I tried it but "std::make_pair(item, total);" (the first line you suggested) gives me this error: " [cquery] no viable conversion from returned value of type 'pair::__type, typename __decay_and_strip::__type>' (aka 'pair') to function return type 'int'" – yeliah Aug 05 '20 at 01:53
  • 2
    Change int processSelection() to std::pair processSelection() – Arundeep Chohan Aug 05 '20 at 02:07
0

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...
    ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you, I have tried this but I am not sure what to put after the it.item = ...; and it.total = ...; – yeliah Aug 05 '20 at 02:05
0

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)
Indranil
  • 136
  • 1
  • 5