Error: run time error-stack around the variable 'food' was corrupted. I can't figure out.

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;


int main()
{
const int num_monkey =3;
const int days =7;
double food[num_monkey][days];
double total1=0,total2=0,total3=0;
double average=0;
double least=0;
double greatest=0;
double sumtotal=0;

cout<<"How much did Monkey 1 eat?"<<endl;
for(int start=0;start<days;start++)
{
cout<<"Day " <<(start+1)<<":";
cin>>food[1][start];
total1 += food[1][start];
}
cout<<"How much did Monkey 2 eat?"<<endl;
for(int start=0;start<days;start++)
{
cout<<"Day " <<(start+1)<<":";
cin>>food[2][start];
total2 += food[2][start];
}
cout<<"How much did Monkey 3 eat?"<<endl;
for(int start=0;start<days;start++)
{
cout<<"Day " <<(start+1)<<":";
cin>>food[3][start];
total3 += food[3][start];
}

double sum[7];

for(int count=0;count<7; count++)
{
sum[count]=food[1][count]+food[2][count]+food[3][count];
sumtotal+= sum[count];
}

average=sumtotal/7;
cout<<"The average amount of food eaten per day is: "<<average<<endl;

//least amount eaten
least=total1;
if(least>total2)
least=total2;
if(least>total3)
least=total3;

//greatest amount eaten
greatest=total1;
if(greatest<total2)
greatest=total2;
if(greatest<total3)
greatest=total3;

cout<<"Least amount of food eaten during the week by any one monkey = "<<least<<endl;

cout<<"Greatest amount of food eaten during the week by any one monkey = "<<greatest<<endl;

system("pause");
return 0;
}
1
2
cin >> food[3][start];
total3 += food[3][start];

Your index 3 is out of bounds. Valid index is between 0-2
Why do you repeat the code for input 3x ?
You could use a for loop instead.
Topic archived. No new replies allowed.