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;