降雨量统计错误最低数量C ++

时间:2016-02-02 21:01:52

标签: c++

Please enter rainfall for month 1:1
Please enter rainfall for month 2:2
Please enter rainfall for month 3:3
Please enter rainfall for month 4:4
Please enter rainfall for month 5:5
Please enter rainfall for month 6:6
Please enter rainfall for month 7:7
Please enter rainfall for month 8:8
Please enter rainfall for month 9:9
Please enter rainfall for month 10:10
Please enter rainfall for month 11:11
Please enter rainfall for month 12:12
Total rainfall was: 78.00
Average rainfall was: 6.50
Highest rainfall was: 12.00
Lowest rainfall was: -92559631349317830000000000000000000000000000000000000000000000.00
Would you like to repeat the program? (Y/N)N
Press any key to continue ...

所以我的代码工作正常,因为没有错误消息,但最低数字的结果是关闭,无法看到原因。有什么想法或帮助吗?事情是我不明白为什么它没有发生在代码的其他部分,除了最低的结果。

这是我的代码。

int64_t

4 个答案:

答案 0 :(得分:3)

  

我的代码工作正常,没有错误消息

这称为“我的代码正确编译”,这是一个很好的第一步。

问题是你的代码已经超过了第12个月:

for (int i = 1; i <= 12; i++)

由于未定义的行为,它不适用于最大值,但是您读取第13个值的位置恰好具有较低的数字。

您可以通过使用i < 12来解决此问题,但这样会对数组的大小进行硬编码。更好的方法是使用std::array<double,12>作为数字,并使用其begin / end迭代器访问各个值,甚至调用min_element以避免编写自己的循环:

array<double,12> rainFall;
...
double highest = *min_element(rainFall.begin(), rainFall.end());
double lowest = *min_element(rainFall.begin(), rainFall.end());

答案 1 :(得分:0)

您的第二个for循环应终止于i<12而不是i<=12

for (int i = 1; i < 12; i++)
{
    rainMonth = rainFall[i];
    if (rainMonth < lowest)
        lowest = rainMonth;
    if (rainMonth > highest)
        highest = rainMonth;
}

答案 2 :(得分:0)

这应该可以正常工作:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int month_array = 12;
double rainFall[month_array];
double yearly = 0;
double lowest;
double highest;
double average;
double rainMonth;
char repeat = 'Y';

//Repeat Loop
while (repeat == 'y' || repeat == 'Y')
{

for (int i = 0; i < month_array; i++)
{
    cout << "Please enter rainfall for month " << (i + 1) << ":";
    cin >> rainFall[i];

    while (rainFall[i] < 0)
    {
        cout << "No negative numbers, Please try again: ";
        cin >> rainFall[i];
    }

    yearly += rainFall[i];
}

average = yearly / 12;

highest = -99999999;
lowest = 99999999;

for (i = 0; i < 12; i++)
{
    rainMonth = rainFall[i];

    if (rainMonth < lowest)
        lowest = rainMonth;
    if (rainMonth > highest)
        highest = rainMonth;
}

cout << "Total rainfall was: " << setprecision(2) << showpoint << fixed << yearly << endl;
cout << "Average rainfall was: " << setprecision(2) << showpoint << fixed << average << endl;
cout << "Highest rainfall was: " << setprecision(2) << showpoint << fixed << highest << endl;
cout << "Lowest rainfall was: " << setprecision(2) << showpoint << fixed << lowest << endl;

cout << "Would you like to repeat the program? (Y/N)";
cin >> repeat;
}
system("pause");
return 0;

答案 3 :(得分:0)

正如其他答案向您展示的那样,您正在尝试访问数组中不存在的第13个元素。请记住,数组从元素0开始而不是1.对于没有经验的程序员来说,这是一个常见的错误。

请记住,您已初始化double rainFall[month_array];const int month_array = 12;。因此,如果您尝试运行for (int i = 1; i <= 12; i++)并使用i访问rainFall数组中的每个元素,那么您已经从元素1开始,这是第二个元素在数组中。

要修复它,您有很多选项,其中一个选项是将 for 循环中的第一行更改为:rainMonth = rainFall[i-1];

但是如果你想保持一致,可以将循环条件改为:

for (int i = 0; i < 12; i++) 

for (int i = 0; i <= 11; i++)

希望有所帮助!

E.g。 for (int i = 1; i <= 11; i++)

这将允许您访问阵列中第一个元素之后的每个元素。