C++ C++;嵌套循环

C++ C++;嵌套循环,c++,loops,nested,C++,Loops,Nested,我必须编写一个程序,要求用户提供年数,然后要求用户提供这些年中每个月的降雨量(单位:mm)。我必须计算总月数、总降雨量、每月平均降雨量、计算所有月份的最大降雨量,并输出月份名称(将月份编号转换为名称)和降雨量最大的年份。到目前为止,我已经编写了这段代码,但是我无法准确地输出确切的月份名称和降雨量最高的年份,即使我已经计算了最高降雨量值 const int numMonths = 12; int numYears, months, largest = 0; double sum = 0; co

我必须编写一个程序,要求用户提供年数,然后要求用户提供这些年中每个月的降雨量(单位:mm)。我必须计算总月数、总降雨量、每月平均降雨量、计算所有月份的最大降雨量,并输出月份名称(将月份编号转换为名称)和降雨量最大的年份。到目前为止,我已经编写了这段代码,但是我无法准确地输出确切的月份名称和降雨量最高的年份,即使我已经计算了最高降雨量值

const int numMonths = 12;
int numYears, months, largest = 0;
double sum = 0;


cout << "Please enter the number of years: ";
cin >> numYears;
cin.ignore();

for (int years = 1; years <= numYears; years ++)
{
    for (int months = 1; months <= numMonths; months ++)
    {
    double rain;
    cout << "Please enter the rainfall in mm for year " << years << ", month " << months << "\n";
    cin >> rain;
    sum += rain;
    if (rain > largest){

        largest = rain;

    }
    cin.ignore();
    }   
}

int totalMonth = numYears*numMonths;
double avgRain = sum / totalMonth;
cout << "Total number of months: " << totalMonth << "\n";
cout << "Total inches of rainfall for the entire period: "<< sum << "\n";
cout << "Average rainfall per month for the entire period: " << avgRain << "\n"; 
cout << "Highest rainfall was " << largest << ;






cin.get();
return 0;
const int numMonths=12;
整数单位,月,最大=0;
双和=0;
颈髓;
cin.ignore();

对于(int years=1;years

   if (rain > largest_rain){        
        largest_rain = rain;
        largest_month = months;
        largest_year = years;
    }

比如说:

   if (rain > largest_rain){        
        largest_rain = rain;
        largest_month = months;
        largest_year = years;
    }

为了将月份数映射到名称,我将它们放在一个字符串数组中

string[] months = {"January","February","March"...};
然后取你的月数(如果你是1索引,则减去1),并将该索引打印到数组中

所有这些看起来都是这样的:

string [] month = {"January","February", "March"/*Fill in the rest of the months*/};
int largestMonthIndex = largest_month-1;  
cout << "Month that the largest rain fall occurred in: " <<month[largetMonthIndex];
string[]月={“一月”、“二月”、“三月”/*填写其余月份*/};
int largestMonthIndex=最大月数1;

cout要将月份数映射到姓名,我会将它们放入字符串数组中

string[] months = {"January","February","March"...};
然后取你的月数(如果你是1索引,则减去1),并将该索引打印到数组中

所有这些看起来都是这样的:

string [] month = {"January","February", "March"/*Fill in the rest of the months*/};
int largestMonthIndex = largest_month-1;  
cout << "Month that the largest rain fall occurred in: " <<month[largetMonthIndex];
string[]月={“一月”、“二月”、“三月”/*填写其余月份*/};
int largestMonthIndex=最大月数1;

是的,但是我怎样才能得到实际的月份名称来显示呢?比如一月、二月等@user566094你需要一个查找表。你可以使用
向量
,因为你的索引是整数(你的偏移量是1)。枚举适合这里:。嘿,你知道我如何执行用户输入验证,以实现用户不能为rain输入负值吗?是的,但我如何获得实际月份名称以显示?如一月、二月等。@user566094你需要一个查找表。你可以使用
向量
,因为你的索引是整数(然后用1抵消)。枚举适合这里:。嘿,你知道我如何执行用户输入验证,实现用户不能为rain输入负值吗?它告诉我“@user566094:这意味着你的源文件缺少
\include
\include
。嘿,你知道我如何执行用户输入验证吗h实现了用户不能为rain输入负值?它告诉我'@user566094:这意味着您的源文件缺少
#include
#include
。嘿,您知道我如何执行用户输入验证来实现用户不能为rain输入负值吗?