c++ - 如何将一个函数中的值用于主函数?
问题描述
struct Month {
string month;
float avg;
};
float calMonth1() {
float sum;
int i;
Month month1;
//month 1
cout << "Weekly Dengue Case for Month of: ";
cin >> month1.month;
int n = 4;
int * week = new int[n];
for (i = 0; i < n; ++i) {
cout << "Week " << i + 1 << ": ";
cin >> week[i];
sum += week[i];
}
month1.avg = sum / i;
int firstWeek = week[0];
cout << "Average for " << month1.month << ": " << month1.avg;
return month1.avg;
}
float calMonth2() {
int i;
float sum;
Month month2;
//month 2
cout << "Weekly Dengue Case for Month of: ";
cin >> month2.month;
int n = 4;
int * week = new int[n];
for (i = 0; i < n; ++i) {
cout << "Week " << i + 1 << ": ";
cin >> week[i];
sum += week[i];
}
month2.avg = sum / i;
cout << "Average for " << month2.month << ": " << month2.avg;
return month2.avg;
}
int main() {
int firstWeek;
calMonth1();
calMonth2();
cout << "\nInformation for first week of January, total of " << firstWeek << " cases:\n";
return 0;
}
这是我的代码。我想要做的是在我的主函数中使用 calMonth1 函数中的 firstWeek 。我怎么可能做到这一点?我只想使用 calMonth1 中数组的第一个值。
示例输出:
第 1 周:10 第 2 周:20 第 3 周:30 第 4 周:40
1月第一周的信息,共10例:
我只希望第一周的值在上述消息中显示。谢谢
解决方案
推荐阅读
- python - 基于另一列的 str.contains 正在返回空字符串的值
- powershell - 使用 powershell 将 base64 编码密码转换为 SecureString
- saved-searches - 我想要在 idx api 中保存的搜索计数
- php - 为什么在 bind_params 上准备好的语句失败?
- amazon-web-services - AWS Cloudwatch 指标警报在第一次后未触发
- jooq - 在 jooq 中重用 DSLContext 时性能下降
- azure-cosmosdb - 收到 HTTP 错误 400。请求标头的大小太长。- Microsoft.Azure.Documents.DocumentClientException
- c - 关闭与数据库的连接时不会释放内存(C,postgres - libpq)
- postgresql - Postgres 是否有一个列出 date_trunc 精度值的系统表?
- python-3.x - 为什么我无法安装 line_profiler?