# Thread: Help Can't figure out what is wrong

1. ## 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. 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. Are those not the right numbers? What are you supposed to be getting?

4. 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. > for(int i=0; i>ro; i++)
Fix the loops?

6. thanks for seeing that. I fixed those and I'm still having the same problem

7. 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. Then what about stuff like this:

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

Is rows a valid subscript, too?

9. I tried that and it crashed the program

10. We know. That's why we were suggesting you take it out, not leave it in.

11. 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. 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. 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.

14. 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.

15. Thank you every one I got it fixed