本帖最后由 yuxijian2020 于 2021-3-24 10:20 编辑
我研究半天没懂你这需求里面哪里有什么函数套函数....
就单纯从第一个main函数来看,这就是写出3个函数的定义就完事了#include <iostream>
using namespace std;
//函数声明
int getInput();
int calcCalories(int sum);
void displayOutput(int sum, int total);
int main ()
{
int number_of_items, total_calories;
number_of_items = getInput();
total_calories = calcCalories(number_of_items);
displayOutput(number_of_items, total_calories);
return 0;
}
int getInput()
{
int sum = 0;
printf_s("How many items did you eat today?\n");
cin >> sum;
//if(sum > 0) //不太确定你这是不是要有个判断
//{
return sum;
//}
}
int calcCalories(int sum)
{
int temp = 0;
int total = 0;
printf_s("Eater the number of calories in each of the %d items eaten:\n", sum);
for(int i = 1; i <= sum; ++i)
{
printf_s("Item %d: ", i);
cin >> temp;
total += temp;
}
return total;
}
void displayOutput(int sum, int total)
{
float average = (float)total / (float)sum;
printf_s("Total calories eaten today = %d\n", total);
printf_s("The average calories per item is %.2f\n", average);
}
|