使用函数返回多个值C++ [duplicate]

yv5phkfx  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(128)
    • 此问题在此处已有答案**:

(23个答案)
Can a C++ function return more than one value? [duplicate](11个答案)
9小时前关门了。
我必须创建一个函数来完成所有的处理(billsHundred到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()
{

}
flvlnr44

flvlnr441#

返回多个值有两种基本方法:返回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);

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

ogq8wdun

ogq8wdun2#

你可以考虑用Map。

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

mklgxw1f3#

为了回答你的问题,在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())可能会做得更好,每个函数只返回一个值。

相关问题