Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: pointer error

  1. #1
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post

    pointer error

    here is the sample of the code not working
    Code:
    #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

    the value of i was 3 from even when the loop of j was entered 1st time cant figure out how 0 became 3
    Last edited by oracleguy; 07-15-2009 at 06:20 PM. Reason: please use code tags

  • #2
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    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:

    Code:
    #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;
    }

  • Users who have thanked BrickInTheWall for this post:

    adarshakb (07-16-2009)

  • #3
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    WOW this should go in the history book

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Can anyone confirm? Kev? Ralph?
    Yeah..
    Code:
    for(int i=0;i<maxrow;i++)
    //do nothing until i==maxrow
    ;
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •