C编程-在main()外部的函数中收集数据

问题描述 投票:0回答:1

编写一个程序,要求用户输入每日降雨量。您的程序将需要接受5个每日降雨量输入。仅允许非负降雨量。当用户输入一个负数时,告诉他们该数字无效,并且应该输入另一个有效值。

计算总降雨量和平均降雨量。确定最大的每日降雨量和最小的每日降雨量。

使用信息性消息输出总,平均值,最大值和最小值。

以下内容不会在main中发生:

  1. 接受用户输入
  2. 总计或平均值的计算
  3. 最大或最小的确定
  4. 输出结果

==============================================>

现在,我只是想弄清楚如何输入这5个数字,到目前为止,我已经有了这段代码,但是却让我无数次地输入了它。我已经在这个项目上工作了几个小时,所以任何建议都会很棒。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times

double CollectRainfall() {
    double amount;
    double rainfall[SIZE];
    int i;

    printf("Enter a rainfall amount: \n");  // enter amount
    scanf_s("%lf", &amount);

    for (i = 0; i < SIZE; i++) {
        rainfall[i] = CollectRainfall();
        while (amount < 0.0) {  // if it's a negative number
            printf("The number is invalid.\n");  // display error message if a negative # was entered
            printf("Enter another rainfall amount: \n");
        }
    }

}   
int main() {

    CollectRainfall();

    return 0;
}  

编写一个程序,要求用户输入每日降雨量。您的程序将需要接受5个每日降雨量输入。仅允许非负降雨量。当用户输入否定的...

c function main
1个回答
0
投票

您可以创建一个结构来存储您的数据并执行操作。

© www.soinside.com 2019 - 2024. All rights reserved.