在上述程序中加入一个函数使其可以计算出猫和老鼠有多重
时间: 2024-05-02 12:21:46 浏览: 6
以下是加入计算体重的函数后的程序:
```
#include <iostream>
using namespace std;
void calculateWeight(double catWeight, double mouseWeight) {
double totalWeight = catWeight + mouseWeight;
cout << "The total weight of the cat and mouse is " << totalWeight << " kg." << endl;
}
int main() {
double catWeight, mouseWeight;
cout << "Please enter the weight of the cat (in kg): ";
cin >> catWeight;
cout << "Please enter the weight of the mouse (in kg): ";
cin >> mouseWeight;
calculateWeight(catWeight, mouseWeight);
return 0;
}
```
在上述程序中,我们定义了一个名为`calculateWeight`的函数,它接受两个参数:`catWeight`和`mouseWeight`,分别表示猫和老鼠的体重。在函数中,我们将这两个参数相加得到总体重,并将结果输出到屏幕上。
在`main`函数中,我们先读入猫和老鼠的体重,然后调用`calculateWeight`函数计算它们的总体重。最后,程序结束并返回0。