![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 4
| Debug Error Help! :D 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);
}
|
| CodyHerrick is offline | |
| | #2 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Compare the size of the arrays you pass to initem to the values you use for those arrays in the for loops in that function. They don't match. Make sure to check your other functions for this as well. Perhaps more descriptive variable names would make it more obvious. |
| Daved is offline | |
| | #3 |
| Registered User Join Date: Nov 2009
Posts: 4
| OMG!!!!!! Thanks So Much!!!!!!! DDD Last edited by CodyHerrick; 11-06-2009 at 12:03 AM. |
| CodyHerrick is offline | |
![]() |
| Tags |
| debug, error, homework |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary not built with debug info - why? | ulillillia | C Programming | 15 | 12-11-2008 01:37 AM |
| makefiles - debug & release? | cpjust | C Programming | 6 | 10-26-2007 04:00 PM |
| Debug --> Exceptions in Visual Studio 2005 | George2 | C# Programming | 1 | 08-10-2007 02:12 AM |
| Results in Debug and Release mode are different | jaro | C Programming | 11 | 05-27-2006 11:08 AM |
| Ask about Debug Assert Failed | ooosawaddee3 | C++ Programming | 0 | 04-24-2002 11:07 PM |