简体   繁体   English

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

[英]Use a function to return multiple values C++

I have to create a function that will do all the processing (the billsHundred to coinLoonie calculation), but input and output to console will still be done in int main.我必须创建一个函数来完成所有处理( billsHundred 到 coinLoonie 计算),但输入和输出到控制台仍将在 int main 中完成。 How should I go about this.我该怎么办。 I am really having an issue with producing multiple outputs from the function int cash() so i left that blank in order to see what you guys would suggest.我真的遇到了从函数 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()
{


}

There are basically two ways to return multiple values.基本上有两种方法可以返回多个值。 You either return a struct or class as the return value, or you pass in reference values or pointers and the function sets the referred to/pointed to value.您可以返回一个structclass作为返回值,或者您传入引用值或指针,然后函数设置被引用/指向的值。

So if you have to break an amount into 10s and 1s:因此,如果您必须将数量分成 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.

Alternatively:或者:

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

int tens;
int ones;
cash(15, tens, ones);

In your application, I would use the struct - a function with seven output arguments has too many arguments.在您的应用程序中,我将使用 struct - 具有七个输出参数的函数有太多参数。

To answer your question, you can't really return multiple values from a function in C++.要回答您的问题,您不能真正从 C++ 中的函数返回多个值。 Instead, you could return a struct or some other object that contains or references multiple things.相反,您可以返回一个结构体或其他一些包含或引用多个事物的对象。 For example:例如:

#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;
}

Based on the rest of the code above, though, I think might do better calling multiple functions ( getBillsHundred() , etc.), or a single function with different parameters ( getBillsBySize() ) for each of the different sizes.不过,基于上面的其余代码,我认为可能会更好地调用多个函数( getBillsHundred()等),或者为每个不同大小调用具有不同参数的单个函数( getBillsBySize() )。 Each of those functions would just return one value.这些函数中的每一个都只会返回一个值。

You can consider using a map.您可以考虑使用地图。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM