Thread: Debug Error Help! :D

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Debug Error Help! :D

    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);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM

Tags for this Thread