C++ - 如何添加函数以处理货币计算器的多种情况
C++ - How do I add functions to work on a multitude of cases for a currency calculator?
For my currency calculator, I need to add 3 different types of functions-
1) enterCostUSD (I enter the cost in USD)
2) convertCost (convert the cost from USD to, say, rupees)
3) displayOutput (display the output of the new cost)
我总共有10个案例。如何添加函数以使其适用于所有 10例?
我是否需要在每个案例的中间添加它们?
这是其中一种情况,我需要以这种方式进行还是有更好的方法?
case 1: //Finding cost in Altairian Dollars
cout << "Enter the cost in US Dollars: ";
cin >> enterCostUSD;
altairianDollars = enterCostUSD*0.72
cout << "$" << enterCostUSD << " is equal to " << altairianDollars
cout << "Thank You for using the Currency Calculator!" << endl;
break;
我从你的问题中了解到的是,你有 10 种情况试图转换货币。因此,为此,您必须创建10个案例,就像您在问题中提到的一样。
如果所有情况通用的任何功能,您可以将其保留在一个函数中并从每个cases.
调用它
在这里,打印输出和转换货币是可以分开的功能。
例如:
case 2: //Finding cost in Rupee
CostUSD=enterUSDCost();//function 1
conversion_rate=63.5;//for rupee
Rupee = convertCost(CostUSD,conversion_rate);//function2
displayOutput(enterCosstUSD,Rupee,"Rs");//function 3
break;
这三个函数需要超出你的主函数的范围,这是三个独立的函数。
float enterCostUSD()
{
float USD_cost;
cout << "Enter the cost in US Dollars: ";
cin >> USD_cost;
return USD_cost;
}
float convertCost(float USD, float rate)
{
return USD*rate;
}
void displayOutput(float enterCostUSD, float Rupee,string conversion_currency){
cout << "$" << enterCostUSD << " is equal to " << Rupee <<conversion_currency ;
cout << "Thank You for using the Currency Calculator!" << endl;
}
如果您想更好地了解开关情况,请点击此链接
相关文章:
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- C++嵌套if语句,基本货币交换
- 使用 BMI 计算器对C++中的数字进行四舍五入的问题
- 我在c ++矩阵计算器程序中找不到错误
- C++计算器,不能单独除以 0
- 如何正确解析子字符串,以便它们对我的新手计算器有效?
- Arduino 基础 2 计算器无法C++工作
- 需要有关我的平均计算器代码的建议
- C++ - 在我尝试制作一个简单的计算器时有一个"uninitialized local variable y used"警告
- 野牛/flex:计算器将双精度值解释为整数,所以我添加了 YYSTYPE 双精度 #define 但我有多个编译错误
- C++计算器运算符
- 使用WIN32 API的击球平均计算器
- 如何提取由 unordered_map::emplace 重新调整的货币对的值?
- RPN计算器c++错误处理和多个运算符
- C++查找用于构建单行计算器
- 如何修复这个基本计算器
- 用于加法和减法的十六进制计算器
- 桌面计算器出现问题.C++程序设计语言
- 如何对堆栈中的所有项目求和?Postfix计算器
- C++ - 如何添加函数以处理货币计算器的多种情况