Mk so, I have to do this program for class,input rainfall for 6 months, total them, output total, average, highest month, and lowest month.
Here's what I've got so far.
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
|
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
char month[6][12]={"January","February","March","April","May","June"};
char holdermonth[6][12]={};
double rain[6];
double rainamt, rainavg,raintot,holdernum;
holdernum=0;
raintot=0;
// input for rainfall amount
for (int n=0;n<6;n++)
{
cout<<"Input Rainfaill amount for the month of "<<month[n]<<endl;
cin>>rainamt;
rain[n]=rainamt;
}
// totaling for 6 months of rain and average
for (int n=0;n<6;n++)
{
raintot=raintot+rain[n];
}
rainavg=raintot/6;
cout<<"raintot="<<raintot<<endl;
cout<<"rainavg="<<rainavg<<endl;
// random cout to make sure month array will output correctly
for (int n=0; n<6;n++)
{cout<<month[n]<<endl;}
//Bubble Sort Ascending Order
for (int n=0; n<6;n++)
{
for (int y=0; y<5; y++)
{
if (rain[y]>rain[y+1])
{
holdernum=rain[y];
rain[y]=rain[y+1];
rain[y+1]=holdernum;
//Problem Area Here VVVVVVVV
holdermonth[y][12]=month[y][12];
month[y][12]=month[y+1][12];
month[y+1][12]=holdermonth[y][12];
//Problem Area There ^^^^^^^^
}
}
}
// cout to test bubble sort
for (int n=0; n<6;n++)
{
cout<<"month "<<month[n]<<" rain "<<rain[n]<<endl;
}
return 0;
}
|
The problem I'm getting is the program errors out when I input a larger number
in any of the 1st 5 array slots than what is in the 6th slot I get:
Run-Time Check Failure #2 Stack around variable 'month' was corrupted.
What am I doing wrong in the code to cause this?
Also, If I have the input for rain[6] looking like this:
[0]=5,[1]=4,[2]=1,[3]=8,[4]=12,[5]=10,[6]=14
No error, and it sorts the numbers correctly, but it only changes the 1st letter of the months. How do I sort char arrays?