Nov 24, 2012 at 10:06pm UTC
heres my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
double avgRain = 0;
double rainSum = 0;
int count = 0;
int monthlyTotals[12];
double wettestPoint = 10000;
double dryestPoint = 0;
string wettestMonth;
string dryestMonth = "January" ;
string monthNames[] = {"January" , "Febuary" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" };
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(2);
cout << "Enter the amount of rainfall for each month.\n\n" ;
for (count = 0; count < 12; count++)
{
cout << monthNames[count] << ": " ;
cin >> monthlyTotals[count];
while (monthlyTotals[count] < 0)
{
cout << "\nEnter a positive number for each month.\n" << endl;
cout << monthNames[count] << ": " ;
cin >> monthlyTotals[count];
}
}
for (count = 0; count < 12; count++)
{
rainSum = rainSum + monthlyTotals[count];
}
avgRain = rainSum / 12;
dryestPoint = monthlyTotals[0];
for (count = 0; count <= 12; count++)
{
if (monthlyTotals[count] >= dryestPoint)
{
dryestPoint = monthlyTotals[count];
dryestMonth = monthNames[count];
}
}
wettestPoint = monthlyTotals[0];
for (count = 0; count <= 12; count++)
{
if (monthlyTotals[count] <= wettestPoint)
{
wettestPoint = monthlyTotals[count];
wettestMonth = monthNames[count];
}
}
cout << "\n" ;
cout << "The total rainfall amount is " << rainSum << endl;
cout << "The average rainfall amount is " << avgRain << endl;
for (count = 0; count < 12; count++)
{
if (monthlyTotals[count] <= dryestPoint)
{
dryestMonth = monthNames[count];
dryestPoint = monthlyTotals[count];
}
}
for (count = 0; count < 12; count++)
{
if (monthlyTotals[count] >= wettestPoint)
{
wettestMonth = monthNames[count];
wettestPoint = monthlyTotals[count];
}
}
cout << "The wettest month and amount is " << dryestMonth << " with a rainfall amount of " << wettestPoint << "." << endl;
cout << "The dryest month and amount is " << wettestMonth << " with a rainfall amount of " << dryestPoint << "." << endl;
cout << endl << "Press ENTER to exit..." ;
cin.clear();
cin.sync();
cin.get();
return 0;
}
When i run the program, right after i enter the last months rainfall, the debugger does a break, and i cant find out why.
Does anyone see why.
Last edited on Nov 24, 2012 at 10:14pm UTC
Nov 24, 2012 at 10:19pm UTC
Simple problem: in some of your loops you iterate over 0-12 inclusive. this means that you end up trying to access array_name[12]
, which doesn't exist (the arrays have only 12 elements)
fix it by changing all the <= signs in the for loops to < signs
Nov 24, 2012 at 10:30pm UTC
for future reference, please try to be certain about where the crash is occurring. You can do this by putting in output lines, for example cout << "checkpoint passed\n" ;
lines at regular intervals (especially after loops) and then recording which one it got up to. You can also use this technique to pinpoint the line at which an error occurs. It makes it much easier to debug if you know exactly where the error occurred.
Nov 25, 2012 at 7:16am UTC
Oh ok, Thank you and i will do that.