使用函数C++返回多个值
Use a function to return multiple values C++
我必须创建一个函数来完成所有处理(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()
{
}
基本上有两种方法可以返回多个值。 您可以返回struct
或class
作为返回值,或者传入引用值或指针,函数设置引用/指向值。
因此,如果您必须将一个金额分成 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()
(的单个函数。这些函数中的每一个都只返回一个值。
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 从python中调用C++函数并获取返回值
- 矩阵向量乘法(cublasDgemv)返回零
- 为什么模板类中的对象不能返回值