2013-12-15 4 views
-6

내 오류 : Myprograms.exe가 작동을 멈췄습니다. 문제로 인해 프로그램에서 correclty 작동이 중지되었습니다. 창에서 해당 프로그램을 닫고 해결 방법이 내가 이러한 오류를 받고 있어요 왜강우량 프로그램 종료시 오류

불확실 사용할 수 있는지 알려줍니다

#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 year\n"; 
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; 

} 
} 
+0

컴파일하고 프로그램을 실행하고 모든 코드가 의도 한대로 실행 나타납니다,하지만 결국에 그것은 포장 마차 내가이 오류 : Myprograms.exe가 중지되었습니다 문제를 해결하면 프로그램의 작동이 멈추었습니다. Windows가 프로그램을 닫고 솔루션을 사용할 수있는 경우 알려줍니다. – user3105593

+1

디버거를 사용하여 프로그램을 단계별로 실행하고 문제가 발생한 정확한 위치를 확인해 보셨습니까? –

+0

MyPrograms.exe의 0x0F56EA8E (msvcr110d.dll) 처리되지 않은 예외 : 0xC0000005 : 0x88EEA7F1.A7F1 위치를 읽는 액세스 위반이 디버거에서 가져온 것임 – user3105593

답변

1

가능한 원인은 displayOutput()에 루프 조건이다.

for (int i = 0; rainArray[i] <= NUM_MONTHS; i++) 

그것은 아마도 rainArray의 모든 요소를 ​​통해 루프를 해야하는 : 여기에 라인입니다. 그러나 현재는 입력 데이터에 따라 임의의 횟수만큼 반복됩니다. 어레이의 끝을 지나가고 액세스하지 않아야하는 메모리에 액세스하려는 가능성이 완전히 있습니다.

오류 메시지가 표시 될 수 있습니다. 비록 당신이 테스트하고있는 입력 데이터가 무엇인지 알지 못한다면 확실하게 알기는 어렵습니다.

이에 루프를 변경해보십시오 :

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

내 테스트 데이터가 4, 2, 3, 7, 5, 3, 2, 4, 4, 3, 4 – user3105593

+1

죄송합니다. 루프 조건은'<'not'<='을 사용해야합니다. 내 대답을 편집 할게. –

+0

바로 그 것이 실제로 효과가있었습니다. 감사합니다. – user3105593

관련 문제