Help?
Error:
Debug Error!
Run-Time Check Failure #2 - Stack Around the variable 'totsold' was corrupted.
Code:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int maxp = 25;
const int maxw = 4;
struct stype
{
int id;
string product;
int quan[maxw];
double uprice;
};
const stype initrec={-1,"Tada",0,0,0,0,0};
void initem(stype s[],int totsold[],int totweek[])
{
int i,q,r;
for (i=0;i <maxp;i++)s[i] = initrec;
for (q=0;q <maxw;q++)totsold[q]=0;
for (r=0;r <maxp;r++)totweek[r]=0;
}
void readem(stype s[])
{
int i,q;
ifstream inf;
inf.open("program5.dat");
for(i=0;i <maxp;i++)
{
inf >> s[i].id
>> s[i].product;
for(q=0;q <maxw;q++) inf >> s[i].quan[q];
inf >> s[i].uprice;
}
}
void calcem(stype s[], int totsold[],int totweek[])
{
int i,q,j,k;
for(q=0;q <maxw;q++)
{
for(i=0;i <maxp;i++)totsold[q]=totsold[q]+s[i].quan[q];
}
for(j=0;j <maxp;j++)
{
for(k=0;k <maxw;k++)totweek[j]=totweek[j]+s[j].quan[k];
}
}
void swapem(stype &a,stype &b)
{
stype temp;
temp=a;
a=b;
b=temp;
}
void sortem(stype s[])
{
int i,j;
for(j=0; j <maxp-1;j++)
for(i=0; i <maxp-1;i++) if(s[i].product > s[i+1].product) swapem(s[i],s[i+1]);
}
void printem(stype s[],ofstream &outf, int totsold[],int totweek[])
{
int i,q,f,g;
for (i=0; i <maxp ;i++)
{
outf << setw(6) << left << s[i].id << setw(12) << left <<s[i].product;
for(q=0;q <maxw;q++) outf << setw(7) << right << s[i].quan[q];
outf << setw(9) << right <<s[i].uprice << setw(9) << totweek[i] <<endl;
}
for(g=0;g <18;g++)
outf<<" ";
for(f=0;f <maxw;f++) outf <<setw(7) << totsold[f];
outf << endl;
}
void main()
{
stype s[maxp];
int totsold[maxp];
int totweek[maxw];
ofstream outf;
outf.open("program5.out");
outf.setf(ios::fixed);
outf.precision(2);
initem(s,totsold,totweek);
readem(s);
calcem(s,totsold,totweek);
sortem(s);
printem(s,outf,totsold,totweek);
}