Help Can't figure out what is wrong

This is a discussion on Help Can't figure out what is wrong within the C++ Programming forums, part of the General Programming Boards category; I don't know what i am doing wrong the out put i get is crazy Code: template<class T> class Prob3Table ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    Help Can't figure out what is wrong

    I don't know what i am doing wrong the out put i get is crazy

    Code:
    template<class T>
    class Prob3Table
    {
    	protected:
    		int rows;                                 //Number of rows in the table
    		int cols;                                 //Number of cols in the table
    		T *rowSum;                                //RowSum array
    		T *colSum;                                //ColSum array
    		T *table;                                 //Table array
    		T grandTotal;                             //Grand total
    		void calcTable(void);                     //Calculate all the sums
    	public:
    		Prob3Table(char *,int,int);               //Constructor then Destructor
    		~Prob3Table(){delete [] table;delete [] rowSum;delete [] colSum;};
    		const T *getTable(void){return table;};
    		const T *getRowSum(void){return rowSum;};
    		const T *getColSum(void){return colSum;};
    		T getGrandTotal(void){return grandTotal;};
    };
    
    template <class T>
    Prob3Table<T>::Prob3Table(char *fle, int r, int c)
    {
        T x;
        grandTotal=0;
        if (r>0){rows=r;}
        else{r=0;}
        if (c>0){cols=c;}
        else{cols=c;}
        ifstream infile(fle);
    	T *table=new T[r*c];
        for(int i=0; i<r; i++)
        {
            for( int j=0; j<c; j++)
            {
                    infile >> x;
                    table[i*c+j]=x;
            }
        }
        cout<<"The origional Table after it is created:"<<endl;
        for(int i=0; i<r; i++)
        {
            for( int j=0; j<c; j++)
            {
                    cout<<table[i*c+j]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    	infile.close();
    
    }
    
    template <class T>
    void Prob3Table<T>::calcTable(void)
    {
        T temp=0;
        rowSum=new T[rows];
        for(int i=0; i<cols; i++)
        {
            rowSum[i]=0;
        }
        colSum=new T[cols];
        for(int i=0; i<cols; i++)
        {
            colSum[i]=0;
        }
    
        for(int i=0; i<cols; i++)
        {
            for(int j=0; j<rows; j++ )
            {
                temp=temp+table[j*cols+i];
            }
            colSum[i]=temp;
            temp=0;
        }
    
        for(int i=0; i<rows; i++)
        {
            for(int j=0; j<cols; j++ )
    
            {
                temp=temp+table[i*cols+j];
            }
            rowSum[i]=temp;
            temp=0;
        }
    
        for(int i=0; i<cols; i++)
        {
            grandTotal=grandTotal+colSum[i];
        }
        cout<<grandTotal;
    }
    
    
    
    
    template<class T>
    class Prob3TableInherited:public Prob3Table<T>
    {
    	protected:
    		T *augTable;                                  //Augmented Table with sums
    	public:
    		Prob3TableInherited(char *,int,int);          //Constructor
    		~Prob3TableInherited(){delete [] augTable;};  //Destructor
    		const T *getAugTable(void){return augTable;};
    };
    
    template <class T>
    Prob3TableInherited<T>::Prob3TableInherited(char *t,int ro,int co) : Prob3Table<T>(t,ro,co)
    {
        this->calcTable();
        augTable= new T[(ro+1)*(co+1)];
    
        for(int i=0; i<ro; i++)
        {
            for( int j=0; j<co; j++)
            {
                augTable[i*co+j]= this->table[i*co+j];
            }
        }
    
        for(int i=0; i>ro; i++)
        {
            augTable[i*co+co]=this->rowSum[i];
        }
    
        for(int i=0; i>co; i++)
        {
            augTable[ro*co+i]=this->colSum[i];
        }
    
        augTable[ro*co]=this->grandTotal;
    
    }
    
    
    int main()
    {
        cout<<"In problem # 3"<<endl<<endl;
        cout<<"Entering problem number 3"<<endl;
    	int rows=5;
    	int cols=6;
    	char file[13]={"Problem3.txt"};
    	Prob3TableInherited<int> tab(file,rows,cols);
    	const int *naugT=tab.getTable();
    	cout<<"The table:"<<endl;
    	for(int i=0;i<rows;i++)
    	{
    		for(int j=0;j<cols;j++)
    		{
    			cout<<naugT[i*cols+j]<<" ";
    		}
    		cout<<endl;
    	}
    	cout<<endl<<"Augmented Table"<<endl;
    	const int *augT=tab.getAugTable();
    	for(int i=0;i<=rows;i++)
    	{
    		for(int j=0;j<=cols;j++)
    		{
    			cout<<augT[i*(cols+1)+j]<<" ";
    		}
    		cout<<endl;
    	}
    }
    the table is all messed up when I run the program and I can't find the logic error

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    I get
    7633012 5 6 2293436 4199597 0

    for the first row of the table when it is displayed in main.
    the rest of the table are numbers like that.

    any time I try to display it out side of the compiler that is what i get

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are those not the right numbers? What are you supposed to be getting?

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    oh right. the table is supposed to be
    100 101 102 103 104 105
    106 107 108 109 110 111
    112 113 114 115 116 117
    118 119 120 121 122 123
    124 125 126 127 128 129

    and above i mean to say any time i try to display the table out side of the *Constructor* I get those weird numbers like i posted before.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > for(int i=0; i>ro; i++)
    Fix the loops?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    thanks for seeing that. I fixed those and I'm still having the same problem

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    The program is supposed to read in a table from a file then add up the rows and columns and find the complete total then add those to the table

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    Then what about stuff like this:

    for(int i=0;i<=rows;i++)

    Is rows a valid subscript, too?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    I tried that and it crashed the program

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We know. That's why we were suggesting you take it out, not leave it in.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Oh I see what you were saying. It works where is is because naugT is the augmented table and it is one row and column bigger than the origional

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you should probably fix all this then:
    Code:
    for(int i=0; i<ro; i++)
        {
            for( int j=0; j<co; j++)
            {
                augTable[i*co+j]= this->table[i*co+j];
            }
        }
    If you want co+1 then you should put co+1.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Attached is my put put I get. the table gets all messed up when I try to display it after leave the constructor. I added the co+1 and i still get this output.
    Attached Images Attached Images  

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Use the debugger, start single-stepping the code at where you think it is going wrong.

    Start with a breakpoint at where you think it is still "good" and then start moving forward slowly (one line at a time) until you see it going wrong.

    Then fix the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Thank you every one I got it fixed

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort+structures: Cant figure what's wrong
    By ERJuanca in forum C Programming
    Replies: 3
    Last Post: 10-13-2009, 01:49 PM
  2. What am I doing wrong ?
    By scottjge in forum C Programming
    Replies: 6
    Last Post: 10-01-2009, 11:55 AM
  3. Recursive maze, so close, but something is wrong
    By forensicgeek in forum C Programming
    Replies: 9
    Last Post: 09-16-2009, 10:48 PM
  4. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 03:18 AM
  5. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 09:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21