c++ - 输出省略 "January"

标签 c++

好的,这个程序的唯一问题是在最后我试图确定哪些月份的降雨总量最低或最高。除非“Jan”(一月)是最高的或最低的,否则我的输出都很好。然后将留空。所有其他变体都很好用。知道为什么会这样吗?我的数组的索引是否在某处?在此先感谢您的任何建议。

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

int main()
{
    //declare and initialize variable and arrays
    string year[] = {"Jan", "Feb", "Mar", "April", "May", "June",
            "July", "August", "Sept", "Oct", "Nov", "Dec"}; 

    const int totalMonths = 12;

    double month[totalMonths]; 

    double average; 
    double total = 0;

    cout << "This program will calculate the total annual rainfall,\n";
    cout << "calculate the monthly average rainfall, and display\n";
    cout << "which month had the lowest amount of rainfall and which\n";
    cout << "month had the highest amount of rainfall. Please enter the\n";
    cout << "rainfall data in inches." << endl;
    cout << "----------------------------------------------------------" << endl;

    // prompt user for input, keep a total sum of data entered
    for(int i = 0; i < 12; i++)
    {
        cout << "Enter total rainfall for " << year[i] << endl;
        cin >> month[i];
        total += month[i];
    while(month[i] < 0)
    {
        cout << "Rainfall cannot be negative!\n";
        cout << "Please re-enter positive numbers" << endl;
        cin >> month[i];
    }       

    }

    //average the total rainfall
    average = total / totalMonths;

    cout << setprecision(1) << fixed;
    cout << "Total Annual rainfall is: " << total << endl;
    cout << "The average rainfall per month is: " << average << endl;

    //determine which month had the lowest and highest amount of rainfall
    double highest = 0;
    string highMonth;
    highest = month[0];
    for(int count = 0; count < totalMonths; count++)
    {
        if(month[count] > highest)
        {
            highest = month[count];
            highMonth = year[count];

        }

    }

    double lowest = 0;
    string lowMonth;
    lowest = month[0];
    for(int count = 0; count < totalMonths; count++)
    {
        if(month[count] < lowest)
        {
            lowest = month[count];
            lowMonth = year[count];
        }
    }

    cout << "The month with the highest rainfall is: " << highMonth << endl;
    cout << "The month with the lowest rainfall is: " << lowMonth << endl;  

return 0;
}

输出示例:

This program will calculate the total annual rainfall,
calculate the monthly average rainfall, and display
which month had the lowest amount of rainfall and which
month had the highest amount of rainfall. Please enter the
rainfall data in inches.
----------------------------------------------------------
Enter total rainfall for Jan
1
Enter total rainfall for Feb
2
Enter total rainfall for Mar
3
Enter total rainfall for April
4
Enter total rainfall for May
5
Enter total rainfall for June
6
Enter total rainfall for July
7
Enter total rainfall for August
89
Enter total rainfall for Sept
12
Enter total rainfall for Oct
13
Enter total rainfall for Nov
14
Enter total rainfall for Dec
15
Total Annual rainfall is: 171.0
The average rainfall per month is: 14.2
The month with the highest rainfall is: August
The month with the lowest rainfall is: <-------- //BLANK!

最佳答案

我在这个程序中发现了两个问题:

  1. 您来这里的原因 -- 您不允许第一个月匹配:

    double highest = 0;
    string highMonth;
    highest = month[0];
    for(int count = 0; count < totalMonths; count++)
    {
        if(month[count] > highest)
        {
            highest = month[count];
            highMonth = year[count];
        }
    }
    

    在这种情况下,month[count] > highest 测试永远不会对 1 月为真。修改测试:

        if(month[count] >= highest)
    

    这允许第一个月匹配。记住这一点,你可能会再次犯这个错误——我知道我犯过。 (另一种情况的修复方法类似。)

  2. 您还没有遇到的另一个问题,但我相信您的教授会注意到:您没有正确处理负数:

    for (int i = 0; i < 12; i++)
    {
        cout << "Enter total rainfall for " << year[i] << endl;
        cin >> month[i];
        total += month[i];
        while (month[i] < 0)
        {
            cout << "Rainfall cannot be negative!\n";
            cout << "Please re-enter positive numbers" << endl;
            cin >> month[i];
        }
    }
    

    如果用户输入负数会怎样? total += month[i] 行将仍然影响总数(因此影响平均值),即使您的输出声称不允许负数。这可能很难在简单的手动测试中发现,除非您也手动计算正确 数字。 (提示:当您可以根据手算结果检查输入时,这几乎总是值得的。)

关于c++ - 输出省略 "January",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9370864/

相关文章:

c++ - 向 QMessageBox 添加滚动条

c++ - 使用 Boost ProgramOptions 处理 2 级命令的最佳方式

c++ - 枚举所有 T 的所有 SomeClass<T> 对象

c++ - 我可以告诉编译器考虑关闭关于返回值的控制路径吗?

c++ - 将 ifstream 复制到 istream C++14

c++ - 为什么在段错误中通过 B 类中的方法初始化 A 类的指针?

c++ - boost::io_service 如何保证处理程序的执行顺序

c++ - 为什么用于字符串比较的 == 运算符相对于任一字符串长度是线性时间(看起来)?

c++ - 将 "pointer to pointer"传递给模板函数

c++ - 除了移动语义之外,还有哪些 C++11 功能可以提高我的代码的性能?