Thread: array of pointers pointing to array of obj

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    array of pointers pointing to array of obj

    hi i was wondering if this is able to be done?
    and if so would this create an array of pointers?

    Code:
    int i, total;
    class house
         { 
                public:
                float height;
                float width;
         }
    int main()
    {
    cout<<"enter total amount of houses\n";
    cin<<total;
    for (i=0;i<total;i++)
         {
               house h[total];
    cout<<"enter height\n"
    cin>>h[i].height;
    cout<<"enter width\n";
    cin>>h[i].width;
         }
    for ( i=0;i<total;i++)
         { 
    house* ph[total];
    ph[i]=&h[i];
         }
    return 0;}
    ive tried using this in a program but i get a few errors:

    cannot convert float* to house* in assignment
    invalid operands of type float* and float to binary 'operator<'
    non l-value in unary &
    invalid operands of type float* and float* to binary 'operator*'
    invalid operands of type float and float* to binary 'operator+'

    im quite new to c++ and not sure what ive done wrong
    thank you in advance

    sorry forgot {} and other stuff this code wont compile just want advice on the errors and the pointer concept
    Last edited by thestien; 10-14-2006 at 09:59 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is this the actual code you are working with? There are several unrelated typos and errors.

    Your code does create an array of pointers as you have it written, but there are some issues. First, you are declaring the h array inside the for block, which means every time you loop through that code a new array is created called h. When you exit that first for loop the h array is destroyed. Then inside the second for loop you try to access the h array, but you can't because it is no longer in scope. You probably want to create the h array before the for loops. The same issue applies to the ph array.

    Also, in C++, it is not yet legal to create an array without a constant size like you have. Your compiler allows it as an extension because the C language allows it, but people who use other C++ compilers won't be able to compile and test your code. In general a vector is preferred over dynamically sized arrays anyway, so you should consider using a vector instead. That won't solve your problem, it is just general advice.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe pick an indentation style which isn't based on the output of rand() ?
    It's only 10 lines, and it's already unreadable.
    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.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Use new:

    Code:
    class house
    { 
    	public:
    	float height;
    	float width;
    }; // forgot the ;
    
    int main( void )      
    {
    	int total;
    
    	cout<< "Enter total amount of houses: ";
    	cin >> total;
    
    	house *ph = new house[total]; // allocates memory
    
    	for ( int i=0; i<total; i++ )
    	{
    		cout<<"\nEnter height: ";
    		cin >>ph[i].height;
    
    		cout<<"\nEnter width: ";
    		cin >>ph[i].width;
    	}
    
    	delete []ph; // never forget to delete memory after allocation
    
    	return 0; // main returns an int. 
    }

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    im really sorry about the coding ill post my proper code here

    Code:
    //this is the main file of my horse betting projest
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    int total;
    int i;
    float accumstake=0.0;
    
    class horse
    { 
          public:
          float odds,stake,stakereturn;
          };
    
    int main () 
    {    
    cout<<"please enter the total amount of bets u need\n";
    cin>>total;
    cin.get();
    horse h[total];
    for (i=0;i<total;i++) //allows user to enter odds 
    {
        cout<<"please enter the odds for the bets in format 1.5\n";
        cin>>h[i].odds;
        cin.get();
    }
    for (i=0;i<total;i++)// sets starting stake at 1
    {
        h[i].stake=1.0;
        h[i].stakereturn=h[i].stake*h[i].odds;//sets all returns
    }
    for (i=0;i<total;i++) //create a pointer for each horse objects
    //stake and stakereturn
    {
    horse *ph[total];
    ph[i]=&h[i].stake;
    
    }
    for (i=0;i<total;i++)//accumalates the total of all stakes of horse objects
    {
        accumstake= accumstake+h[i].stake;
    }
    
    
    
    for (i=0;i<total;i++)//levels through the returns once
    {
        while (&h[i].stakereturn<accumstake)
        {
              &h[i].stake++;
              &h[i].stakereturn=&h[i].stake*&h[i].odds;
              accumstake=accumstake+&h[i].stake;
              cout<<"no."<<i<<" "<<&h[i].stake<<"\n";
              system ("pause");
             // if (h[i].stakereturn<accumstake)
            // { i=0;}
              
              }
    
    }
          //need this to check each h[] to see if the return is 
    //higher than the accumstake in all h[] objects
     cout<<"these are the stakes u need\n";
    for (i=0;i<total;i++)
    {
        cout<<"horse no. "<<i<<" ("<<h[i].stake<<")"<<"\n";
        cout<<"return from horse  no. "<<i<<" "<<"("<<h[i].stakereturn<<")"<<"\n";
    
        }
            cout<<"total stake"<<accumstake<<"\n";
        system ("pause");
    return 0;}
    if i take the arry of pointer part out it does compile but doesnt do what i need it to
    i dont know if you can get the jist of what needs to happen from the code if not post and ill enlighten you

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> horse *ph[total];
    That line of code is still inside a for loop and still goes out of scope when the loop ends.

    I don't know why you need an array of pointers. You use the '&' address-of operator later in the code but I don't see why it is necessary. What makes you say you need an array of pointers?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ok here is the code without pointers and your probly right i dont think i need them but it wont work with out some kind of some thing else lol
    Code:
    //this is the main file of my horse betting projest
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    int total;
    int i;
    float accumstake=0.0;
    
    class horse
    { 
          public:
          float odds,stake,stakereturn;
          };
    
    int main () 
    {    
    cout<<"please enter the total amount of bets u need\n";
    cin>>total;
    cin.get();
    horse h[total];
    for (i=0;i<total;i++) //allows user to enter odds 
    {
        cout<<"please enter the odds for the bets in format 1.5\n";
        cin>>h[i].odds;
        cin.get();
    }
    for (i=0;i<total;i++)// sets starting stake at 1
    {
        h[i].stake=1.0;
        h[i].stakereturn=h[i].stake*h[i].odds;//sets all returns
    }
    for (i=0;i<total;i++)//accumalates the total of all stakes of horse objects
    {
        accumstake= accumstake+h[i].stake;
    }
    
    for (i=0;i<total;i++)//levels through the returns once
    {
        while (h[i].stakereturn<accumstake)
        {
              h[i].stake++;
              h[i].stakereturn=h[i].stake*h[i].odds;
              accumstake=accumstake+h[i].stake;
              if (h[i].stakereturn<accumstake)
              i=0;}
              }
    
    
          //need this to check each h[] to see if the return is 
    //higher than the accumstake in all h[] objects
     cout<<"these are the stakes u need\n";
    for (i=0;i<total;i++)
    {
        cout<<"horse no. "<<i<<" ("<<h[i].stake<<")"<<"\n";
        cout<<"return from horse  no. "<<i<<" "<<"("<<h[i].stakereturn<<")"<<"\n";
    
        }
            cout<<"total stake"<<accumstake<<"\n";
        system ("pause");
    return 0;}



    this part
    Code:
    for (i=0;i<total;i++)//levels through the returns once
    {
        while (h[i].stakereturn<accumstake)
        {
              h[i].stake++;
              h[i].stakereturn=h[i].stake*h[i].odds;
              accumstake=accumstake+h[i].stake;
              if (h[i].stakereturn<accumstake)
              i=0;}
              }
    needs to loop through like this

    Code:
    while (h[i].stakereturn<accumstake)
        {
              h[i].stake++;
              h[i].stakereturn=h[i].stake*h[i].odds;
              accumstake=accumstake+h[i].stake;
              if (h[i].stakereturn<accumstake  ||
     h[i].stakereturn<accumstake ||
     h[i].stakereturn<accumstake)
    // not [i] but [0]------all the way to------[total]
              i=0;}
              }
    but i cant think how to write it as the array is
    undefined in amount of elements

    maybe i can use another for loop but i start to get confused lol
    Last edited by thestien; 10-14-2006 at 10:19 AM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't have time right now to understand exactly what you are trying to do, but perhaps just a nested for loop running from j = 0 to total with the if statement inside it would work.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    Code:
       if ( for (i=0;i<total;i++)
              h[i].stakereturn<accumstake); )
              i=0;}
    i was thinking along those lines but u think the opposite way. ok ill try to write it like that see what happens

    it now looks like this but still doesnt work
    Code:
    for (i=0;i<total;i++)//levels through the returns once
    {
        while (h[i].stakereturn<accumstake)
        {
              h[i].stake++;
              h[i].stakereturn=h[i].stake*h[i].odds;
              accumstake=accumstake+h[i].stake;
              for (int j=0;j<total;j++)
              {if (h[i].stakereturn<accumstake)
              i=0;}
              }
              }
    Last edited by thestien; 10-14-2006 at 10:30 AM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are already inside of a loop using i, so don't use i (which is why I suggested j). Your idea was a good start, but since you can't put a for loop inside an if expression you have to use my way to achieve the same effect.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    could i put another for loop under the while loop which is seperate to the while loop?

    sorry all this looping is really confusing me
    Code:
    for (i=0;i<total;i++)//levels through the returns once
    {
        while (h[i].stakereturn<accumstake)
        {
              h[i].stake++;
              h[i].stakereturn=h[i].stake*h[i].odds;
              accumstake=accumstake+h[i].stake;
              i=0;
             
        }
        
      
    }
    i now have this but it doesnt seem to work right.
    what i want is the code to get to the i=0 part after it has incremented stake?
    is that happening or is assigning i=0 before that?
    ok i just relized that what i done does nothing because the for loop is only initilized once
    so does my i=0 part at the end of the while loop do anything?
    Last edited by thestien; 10-14-2006 at 11:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching an array with pointers
    By pxleyes in forum C Programming
    Replies: 16
    Last Post: 03-23-2004, 05:07 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  4. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM