降雨程序结束时出错
Error at end of rainfall program
我的错误:Myprograms.exe已停止工作一个问题导致程序停止正常工作。windows将关闭程序并通知您是否有可用的解决方案
不确定为什么我会得到这样一个错误
#include <iostream>
#include <string>
//Constant to hold number of months
const int NUM_MONTHS = 12;
void getInput(double[]);
double getTotal(double[]);
double getAverage(double, double[]);
double getHighest(double[]);
double getLowest(double[]);
void displayOutput(double[], double, double);
int main()
{
double rainArray[NUM_MONTHS], totalRainfall, averageRainfall, highest, lowest;
//call function getInput
getInput(rainArray);
//call function getTotal
totalRainfall = getTotal(rainArray);
//call function getAverage
averageRainfall = getAverage(totalRainfall, rainArray);
//call function getHighest
highest = getHighest(rainArray);
//call function getLowest
lowest = getLowest(rainArray);
//call function displayOutput
displayOutput(rainArray, highest, lowest);
return 0;
}
//function getInput - precondition - prompt the user to input 12 values and store them in a array(rainArray as parameter)
//Make sure user cannot enter negative number(use while loop)
//postcondition - the array has been populated
void getInput(double rainArray[])
{
using namespace std;
int counter=0, input;
cout << "Enter the amount of rainfall for each month of the yearn";
while (counter < NUM_MONTHS)
{
cin >> input;
if (input > 0)
{
rainArray[counter] = input;
counter++;
}
else
{
cout << "Rainfall must be greater than 0.n";
cout << "Please enter correct total.n";
}
}
}
//function getTotal - precondition - access to array of 12 values(rainArray as parameter)
//Accumulate the monthly values(using a for loop)
//postcondition - returns the total rainfall for the year
double getTotal(double rainArray[])
{
using namespace std;
double total = 0;
for (int count = 0; count < NUM_MONTHS; count++)
total += rainArray[count];
return total;
}
//function getAverage - precondition - access to total rainfall(totalRainfall as parameter)
//Divide from the total from the number of months
//postcondition - returns the total average for the year
double getAverage(double totalRainfall, double rainArray[])
{
using namespace std;
double average = 0;
average = getTotal(rainArray) / NUM_MONTHS;
return average;
}
//function getHighest - precondition - access to array of 12 values(rainArray as paramter)
//Store highest value from rainArray in to variable highestRainfall and return(using for loop)
//post condition - returns the highest amount of rainfall for the year
double getHighest(double rainArray[])
{
double highest;
highest = rainArray[0];
for (int month = 1; month < NUM_MONTHS; month++)
{
if (rainArray[month] > highest)
{
highest = rainArray[month];
}
}
return highest;
}
//function getLowest - precondition - access to array of 12 values(rainArray as parameter)
//Store lowest value from rainArray in to variable lowestRainfall and return(using for loop)
//postcondition - returns the lowest amount of rainfall for the year
double getLowest(double rainArray[])
{
double lowest;
lowest = rainArray[0];
for (int month = 1; month < NUM_MONTHS; month++)
{
if (rainArray[month] < lowest)
{
lowest = rainArray[month];
}
}
return lowest;
}
//function displayOutput - preconditon - needs to know the array and the averageRainfall
//Use for loop to display the amount of months and subtract each array element by the averageRainfall
//postcondition - outputs the month number and the difference between rain amount and the average
void displayOutput(double rainArray[], double highest, double lowest)
{
using namespace std;
string monthArray[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
cout << "The highest score is " << highest << endl
<< "The lowest score is " << lowest << endl;
for (int i = 0; rainArray[i] <= NUM_MONTHS; i++)
{
double average = getAverage(getTotal(rainArray), rainArray);
double variance = rainArray[i] - average;
string month = monthArray[i];
cout << "Rainfall for the month of " << month << ": " << rainArray[i]
<< " and is " << variance << "away from the average of " << average << endl;
}
}
一个可能的罪魁祸首是displayOutput()
中的循环条件。这是一条线:
for (int i = 0; rainArray[i] <= NUM_MONTHS; i++)
据推测,它应该循环通过rainArray
中的每个元素。然而,目前它将根据您的输入数据循环任意次数。它完全有可能超过了数组的末尾,并试图访问它不应该访问的内存。
这可以解释您看到的错误信息。但是,如果不知道测试的输入数据是什么,就很难确定。
尝试将循环更改为:
for (int i = 0; i < NUM_MONTHS; i++)
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 为什么在这个代码结束循环中没有得到结束
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 试图对缓存进行跨步测试,但程序并没有结束
- 用pybind11包装C++抽象类时出错
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 降雨程序结束时出错
- boost::shared_ptr 课结束时出错