Я пишу код и столкнулся с ошибкой, когда мне нужно вернуть несколько значений в main()
из другой функции.
Здесь я пытаюсь вернуть item
и total
в main()
функция. Однако я получаю предупреждение о том, что item
не использовался, но я использую его в main()
, где затем говорится «использование необъявленного идентификатора» вместе с total
.
Может кто-нибудь поможет мне с моей проблемой синтаксиса?
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;
Отредактированный код: (Я все еще получаю сообщение об ошибке для return std::make_pair(item, total);
и 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;