提问人:Mr. Puli 提问时间:6/2/2023 最后编辑:Mr. Puli 更新时间:6/2/2023 访问量:89
我是否需要将变量从 main() 模块传递到另一个模块才能获得返回值?
Do I need to pass a variable from the main() module to another module to get a return value?
问:
我将在秋天上 c++ 课程,所以我决定做一些自学。我在上个学期上了一门编程逻辑和设计课,一直在写 pusdo 代码。但现在我想把它翻译成 C++。
我写了一个程序来计算超额提取的费用。目标是用它来练习在模块之间传递变量。
我让程序工作,但我想知道我那里是否有额外的代码。我想将变量返回到主程序,而无需将变量从 main() 传递到其他模块。
我需要这两个变量才能使我的程序正常工作吗?
double account余额=0; double overDrawn=0;如果你们有任何其他提示让我的代码更干净,请告诉我。谢谢!
#include <iostream>
#include <cmath>
using namespace std;
// Page 83
// Exercise 5
// The program determines a monthly checking account fee.
//Declaration of all the modules
int acctBalInput(double acct);
int overdrawInput(double draw);
int feeCalculation(int bal, int draw);
void display(int bal, int draw, int fee);
//Main function
int main()
{
//Declarations
double accountBalance=0;
double overDrawn=0;
double balance;
double drawn;
double totalFee;
balance = acctBalInput(accountBalance);
drawn = overdrawInput(overDrawn);
totalFee = feeCalculation(balance, drawn);
display(balance, drawn, totalFee);
return 0;
}
//Input account balance.
int acctBalInput(double acct)
{
cout << "Enter account balance: ";
cin >> acct;
return acct;
}
//Input overdrawn times.
int overdrawInput(double draw)
{
cout << "Enter the number of times over drawn: ";
cin >> draw;
return draw;
}
//Calculates the total fee.
int feeCalculation( int bal, int draw)
{
int fee;
int feePercent = 0.01;
int drawFee = 5;
fee =(bal*feePercent)+(draw*drawFee);
return fee;
}
//Displays all the ouput.
void display(int bal, int draw, int fee)
{
cout <<"Balance: "<< bal<< endl
<<"Overdrawn: "<< draw << endl
<<"Fee: " << fee << endl;
return;
}
我尝试在谷歌上搜索,以找到更好的方法来编写代码。
答: 暂无答案
评论
acctBalInput
overdrawInput
int
double
double
main