使用函数C++返回多个值

Use a function to return multiple values C++

本文关键字:返回 C++ 函数      更新时间:2023-10-16

我必须创建一个函数来完成所有处理(billsHundred to coinLoonie 计算(,但控制台的输入和输出仍将在 int main 中完成。我应该怎么做。我确实在从函数int cash((生成多个输出时遇到了问题,所以我将其留空,以便查看你们的建议。

#include <iostream>
#include <cmath> 
#include <iomanip>
using namespace std;
int cash();
int main()
{
    int dollarAmount;
    for (int i = 1; i <= 3; i++)
    {
        cout << "Enter the total dollar amount: $";
        cin >> dollarAmount;
        while (cin.fail())
        {
            cout << "nThat entry is not valid. Please try again: ";
            cin.clear();
            cin.ignore(1000, 'n');
            cin.clear();
            cin >> dollarAmount;
        }
        int billsHundred = dollarAmount / 100;
        int billsFifty = (dollarAmount - (100 * billsHundred)) / 50;
        int billsTwenty = (dollarAmount - (100 * billsHundred) - (50 *  billsFifty)) / 20;
        int billsTen = (dollarAmount - (100 * billsHundred) - (50 * billsFifty) - (20 * billsTwenty)) / 10;
        int billsFive = (dollarAmount - (100 * billsHundred) - (50 * billsFifty) - (20 * billsTwenty) - (10 * billsTen)) / 5;
        int coinToonie = (dollarAmount - (100 * billsHundred) - (50 * billsFifty) - (20 * billsTwenty) - (10 * billsTen) - (5 * billsFive)) / 2;
        int coinLoonie = (dollarAmount - (100 * billsHundred) - (50 * billsFifty) - (20 * billsTwenty) - (10 * billsTen) - (5 * billsFive) - (2 * coinToonie)) / 1;
        cout << "nNumber of 100$ bills = " << billsHundred;
        cout << "nNumber of 50$ bills = " << billsFifty;       
        cout << "nNumber of 20$ bills = " << billsTwenty;      
        cout << "nNumber of 10$ bills = " << billsTen;     
        cout << "nNumber of 5$ bills = " << billsFive;     
        cout << "nNumber of Toonies = " << coinToonie;     
        cout << "nNumber of Loonies = " << coinLoonie << endl << endl;
    }
    cout << endl;
    return 0;
}
int cash()
{

}

基本上有两种方法可以返回多个值。 您可以返回structclass作为返回值,或者传入引用值或指针,函数设置引用/指向值。

因此,如果您必须将一个金额分成 10 秒和 1 秒:

struct Change {
    int Tens;
    int Ones;
};
Change cash(int amount) {
    Change result;
    result.Tens = amount / 10;
    result.Ones = amount % 10;
    return result;
}
Change broken = cash(15);
// Refer to broken.Tens and broken.Ones.

或者:

void cash(int amount, int& tens, int& ones) {
    tens = amount/10;
    ones = amount%10;
}
int tens;
int ones;
cash(15, tens, ones);

在您的应用程序中,我会使用结构 - 具有七个输出参数的函数具有太多参数。

您可以考虑使用地图。

std::map<string, int> cash(int dollarAmount) {
    std::map<string, int> ret;
    ret['hundred'] = dollarAmount / 100;
    // stuff similar
    return ret;
}

要回答你的问题,你不能真正从C++中的函数返回多个值。相反,您可以返回包含或引用多个事物的结构或其他对象。例如:

#include <iostream>
struct Foo {
    int x;
    int y;
};
Foo doSomething() {
    Foo f;
    f.x = 10;
    f.y = 20;
    return f;
}
int main()
{
    Foo f = doSomething();
    std::cout <<  f.x << std::endl;
    std::cout << f.y << std::endl;
}

不过,基于上面的其余代码,我认为调用多个函数(getBillsHundred()等(可能会更好,或者为每个不同的大小调用具有不同参数(getBillsBySize()(的单个函数。这些函数中的每一个都只返回一个值。