...

View Full Version : pointer error



adarshakb
07-15-2009, 06:00 PM
here is the sample of the code not working


#include <iostream.h>
class matrix
{
private:
int maxrow,maxcol;
int *ptr;
public:
matrix(int r,int c)
{
maxrow=r;
maxcol=c;
ptr=new int[r*c];
}

void getmat()
{
int tmp=0;
cout<<"\nEnter a matrix of "<<maxrow<<" row and "<<maxcol<<"col\n";
for(int i=0;i<maxrow;i++);
{
for(int j=0;j<maxcol;j++)
{
tmp=maxcol*i;
cin>>ptr[tmp+j];
}
}
}
////////////still a lot of code deleted......


void main()
{
matrix a(3,3);
matrix b(3,3);
a.getmat();
b.getmat();
a.printmat();
b.printmat();
cout<<"\na=a+b;";
a=a+b;
cout<<"\na=a+b;";
a=a-b;
cout<<"\na=a+b;";
a=a*b;

}



I cant enter the matrix.. ONLY 3 numbers are being accepted
and when i checked in the debuger...i had a surprise:rolleyes:

the value of i was 3 from even when the loop of j was entered 1st time:confused: cant figure out how 0 became 3

BrickInTheWall
07-16-2009, 05:43 PM
honestly I don't know what the problem is...I must be stupid or something but I can't find it...I was able to fix it however using a while loop:



#include <iostream>

using namespace std;

class matrix
{
public:
int maxrow,maxcol;
int *ptr;
public:
matrix(int r,int c)
{
maxrow=r;
maxcol=c;
ptr=new int[r*c];
}

~matrix()
{
delete[] ptr;
}

void getmat()
{
int tmp=0;
cout<<"\nEnter a matrix of "<<maxrow<<" row and "<<maxcol<<"col\n";

int i;
i = 0;
while( i < maxrow )
{
for(int j=0;j<maxcol;j++)
{
tmp=maxcol*i;
cin>>ptr[tmp+j];
}
i++;
}
}
};


int main(void)
{
matrix a(3,3);
matrix b(3,3);
a.getmat();
b.getmat();

int endP;
cin >> endP;
return 0;
}

adarshakb
07-16-2009, 07:54 PM
:eek: WOW this should go in the history book:thumbsup:

Fou-Lu
07-17-2009, 09:43 AM
for(int i=0;i<maxrow;i++);
I assume C++ is the same? ; at the end of a for loop is a single execution, and will ignore any subsequent checks against the condition.

Can anyone confirm? Kev? Ralph?

abduraooft
07-17-2009, 11:03 AM
Can anyone confirm? Kev? Ralph? Yeah..

for(int i=0;i<maxrow;i++)
//do nothing until i==maxrow
;
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum